UVA548-Tree(二叉树数组表示)
Problem UVA548-Tree
Accept: 2287 Submit: 13947
Time Limit: 3000 mSec
Problem 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
题解:这个题有两个有价值的地方,一个是二叉树的数组实现,一个是通过中序和后序遍历构造处二叉树的先序遍历。
二叉树的数组实现也是递归的方式,在这个地方由于题目中说明节点权值都不一样而且权值的范围也很适合作为数组下标,因此直接用权值作为节点下标建树。
通过中序和后序遍历构造先序遍历建树并不困难,只要熟悉三种遍历的过程,就很容易找到方法。后序遍历的最后一个必定是根节点,然后他的前面,一部分是左子树,一部分是右子树,
这个时候问题就是精确到哪一位是左子树,利用中序遍历轻松搞定,在中序遍历中找到根节点,左边就是左子树,右边就是右子树,那各有几个就很清楚了,之后就交给递归就好了,需要注意递归基,
也就是在中序遍历序列中找不到子树区间的时候,也就是左端点 > 右端点的时候。
一般情况下,用数组实现二叉树需要自己“申请”节点,也就是和链式前向星里的tot++差不多的操作
1 const int root = 1; 2 int cnt; 3 4 void newtree(){ 5 lchild[root] = rchild[root] = 0; 6 have_val[root] = false; 7 cnt = root; 8 } 9 10 int newnode(){ 11 int u = ++cnt; 12 lchild[u] = rchild[u] = 0; 13 have_val[u] = false; 14 return u; 15 }
摘自紫书。
以下是该题代码
1 #include <iostream> 2 #include <string> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <sstream> 6 #include <algorithm> 7 #define INF 0x3f3f3f3f 8 using namespace std; 9 10 const int maxn = 10000+10; 11 int in_order[maxn],post_order[maxn]; 12 int lchild[maxn],rchild[maxn]; 13 int n,ans,Min; 14 15 bool read_list(int *num){ 16 string str; 17 if(!getline(cin,str)) return false; 18 stringstream ss(str); 19 n = 0; 20 int x; 21 while(ss >> x){ 22 num[n++] = x; 23 } 24 return n > 0; 25 } 26 27 int build(int l1,int r1,int l2,int r2){ 28 if(l1 > r1) return 0; 29 int root = post_order[r2]; 30 int i = l1; 31 while(in_order[i] != root) i++; 32 int cnt = i-l1; 33 lchild[root] = build(l1,i-1,l2,l2+cnt-1); 34 rchild[root] = build(i+1,r1,l2+cnt,r2-1); 35 return root; 36 } 37 38 void dfs(int root,int val){ 39 val += root; 40 if(!lchild[root] && !rchild[root]){ 41 if(Min == val) ans = ans < root ? ans : root; 42 else if(val < Min) ans = root,Min = val; 43 return; 44 } 45 if(val > Min) return; 46 if(lchild[root]) dfs(lchild[root],val); 47 if(rchild[root]) dfs(rchild[root],val); 48 } 49 50 int main() 51 { 52 //freopen("input.txt","r",stdin); 53 while(read_list(in_order)){ 54 read_list(post_order); 55 ans = 0,Min = INF; 56 build(0,n-1,0,n-1); 57 dfs(post_order[n-1],0); 58 printf("%d\n",ans); 59 } 60 return 0; 61 }