[2016-02-08][UVA][548][Tree]

UVA - 548
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 Status

Description

Download as PDF
 

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

 

Input 

The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

 

Output 

For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

 

Sample Input 

 

3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255

 

Sample Output 

 

1
3
255

 

 


Miguel A. Revilla
1999-01-11

 

 

  • 时间:2016-02-08 16:43:28 星期一
  • 题目编号:UVA 548
  • 题目大意:已知二叉树中序遍历和后序遍历得到的点权序列,输出根到叶子的权值最小的点的权值
  • 分析:根据两个序列,生成二叉树,并在中间过程取最优解
  • 方法:递归,dfs

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
const int maxn = 10000 + 100;
int in[maxn],post[maxn];
char tmp[maxn * 7];
int res,hysum;
void dfs(int L1,int R1,int L2,int R2,int sum){
        if(L1 == R1 &&  hysum > sum + in[L1] ){
                res = in[L1];
                hysum = sum + in[L1];
        }
        if(L1 >= R1)    return ;
 
        //find root in inorder
        int root = post[R2];  
        int i = 0;
        while(in[i] != root)    i++;
 
        //len of lson: i - L1
        dfs(L1,i - 1,L2,L2 + i - L1 - 1,sum + root);
        dfs(i + 1,R1,L2 + i - L1,R2 - 1,sum + root);
}
int main(){
        while(gets(tmp)){
                int a,i = 0;
                char *p = tmp;
                while( ~sscanf(p,"%d",&a)){
                        in[i++] = a;
                        do{
                                p++;a /= 10;
                        }while(a);
                        while(*p && isspace(*p))        p++;
                }
                gets(tmp);
                i = 0,p = tmp;
                while( ~sscanf(p,"%d",&a)){
                        post[i++] = a;
                        do{
                                p++;a /= 10;
                        }while(a);
                        while(*p && isspace(*p))        p++;
                }
 
                res = maxn;
                hysum = maxn * maxn;
 
                dfs(0,i - 1,0,i - 1,0);
                printf("%d\n",res);
        }
    return 0;
}

 

 



来自为知笔记(Wiz)



posted on 2016-02-08 18:48  红洋  阅读(274)  评论(0)    收藏  举报

导航