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

UVA - 548

Tree

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

Submit Status

Description

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)

时间: 2024-10-26 03:58:23

[2016-02-08][UVA][548][Tree]的相关文章

UVa 548 Tree(建树,递归遍历)

题意  给你一个树的中序遍历和后序遍历  某个节点的权值为从根节点到该节点所经过节点的和  求权值最小的叶节点的值  如果存在多个  输出值最小的那个 把树建好就好说了  递归递归dfs msun保存最小叶节点权值  ans保存答案 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int maxn = 10005, INF = 0x3f3f3f3f; int i

UVa 548 Tree (建树+前序后序)

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

2016.02.08 值此新年,追梦想

虽然是新年,但已经到了公历的二月份,六分之一已过,离今年的目标有多远?问起自己的目标,一脸茫然,到底目标是什么?梦想又是什么? 每一个季节都需要目标,奋斗的过程需要目标来砥砺自己,丧失目标就是丧失勇气. 梦想不应该在世俗中销声匿迹,相反,梦想应该摆脱世俗的牵绊,作为人生的灯塔. 针对于学习和工作,首要的目标是成为浙江大学优秀的毕业生,让自己的研究生生涯圆满落幕,这是最基本的,也是最需要关注的.对于在蘑菇街的工作和学习,希望自己能够鼓足勇气来学习.不断学习,要知道,只有自己的技术.理论层次高了,才

UVa 548 Tree【二叉树的递归遍历】

题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困难啊啊啊啊 后来终于问懂一丢丢了--- 比如说样例: 中序遍历:3 2 1 4 5 7 6 后序遍历:3 1 2 5 6 7 4 首先做第一层: 在后序遍历中的最后一个数为根节点,然后在到中序遍历中找到这个根节点,在这个根节点的左边是左子树,右边是右子树,这样就确定出了左子树和右子树的区间 然后做第

Uva 548 Tree

0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("built:\n"); if(L1>R1) return 0;//空树 int root=post_order[R2]; int p=L1; while(in_order[p] != root) p++; int cnt = p-L1;//左子树的结点个数 lch[root]=build(L1,p-

UVA - 548 Tree(二叉树的递归遍历)

题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<itera

Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

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

UVA 548(二叉树重建与遍历)

J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-05-16) Description  Tree  You are to determine the value of the leaf node in a given binary tree that is the ter

UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后输出来叶子节点. 一开始写的时候是用gets读入的,报CE, 要用fgets写,关于fgets(),传送门: fgets函数及其用法,C语言fgets函数详解 一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs... 代码: 1 //二叉树的中序和后序遍历还原树并输出最短路径的叶子