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 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
13
255

题目的意思是给你一个树的中序和后序遍历的情况,让你找出从根节点走到叶子节点经过的节点的权值和最小,输出这条路径的叶子节点的权值,如果最小的路径有多种则输出权值最小的叶子节点。

这个题输入处理比较奇葩,网上找了一个模板,用来读入,然后就是普通的递归建树,最后再dfs就行了。

代码如下:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 #define M 100050
 5 #define inf 0x3f3f3f3f
 6 struct node
 7 {
 8     struct node *left;
 9     struct node *right;
10     int v;
11 };
12 node *root;
13 int in[M],post[M],top,min_sum,ans;
14 char s[M];
15 int find(int *a,int n,int c)
16 {
17     for (int i=n-1;i>=0;i--)
18     if (a[i]==c)
19     return i;
20     return 0;
21 }
22 node *build(int n,int *in,int *post)
23 {
24     if (n<=0)
25     return NULL;
26     int p=find(in,n,post[n-1]);
27     node *root=(node *)malloc(sizeof(node));//将malloc(分配空间)的返回值强转成 node *型指针变量
28     root->v=post[n-1];
29     root->left=build(p,in,post);
30     root->right=build(n-p-1,in+p+1,post+p);
31     return root;
32 }
33 void dfs (node *root,int sum)
34 {
35     if (root==NULL)
36     return;
37     sum+=root->v;
38     if (root->left==NULL&&root->right==NULL)
39     {
40         if (min_sum>sum)
41         {
42             min_sum=sum;
43             ans=root->v;
44         }
45         else if (min_sum==sum)
46         ans=min(ans,root->v);
47         return ;
48     }
49     dfs(root->left,sum);
50     dfs(root->right,sum);
51 }
52 int init (char *s,int *a)
53 {
54     int top=0;
55     for (int i=0;s[i];++i)
56     {
57         while (s[i]==' ')
58         i++;
59         a[top]=0;
60         while (s[i]&&isdigit(s[i]))
61         {
62             a[top]=a[top]*10+s[i]-'0';
63             ++i;
64         }
65         top++;
66         if (!s[i])
67         break;
68     }
69     return top;
70 }
71 int main()
72 {
73     //freopen("de.txt","r",stdin);
74     while (gets(s))
75     {
76         init(s,in);
77         gets(s);
78         top=init(s,post);
79         node *root;
80         root=build(top,in,post);
81         min_sum=inf;
82         ans=min_sum;
83         dfs(root,0);
84         printf("%d
",ans);
85     }
86     return 0;
87 }
原文地址:https://www.cnblogs.com/agenthtb/p/5925564.html