UVA548 tree (traversal of binary trees)

Source: Internet
Author: User

Determine the value of the leaf node in a given binary tree that's 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 would contain a description of the binary tree given as the inorder and Postorder traversal

sequences of that tree. Your program would read the until end of file from the input file. The first

Line would contain the sequence of values associated with an inorder traversal of the tree and the second

Line would contain the sequence of values associated with a postorder traversal of the tree. All values

'll is different, greater than zero and less than 10000. Assume that no binary tree would 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 A 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

"Analysis" well began to learn the binary tree, the feeling is very difficult, this code is not very understand, it feels like the first through the middle sequence traversal, order traversal to find the sequence traversal, found sub-leaf point sum.

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<functional>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll;Const intn= -;Const intm=15005;Chars[100005];intv1[100005],v2[100005],top;intMin_sum,ans;intInitChar*s,int*v) {inttop=0;  for(intI=0; T[n]; i++) {         while(s[i]==' ') I++; V[top]=0;  while(s[i]&&isdigit (S[i])) {V[top]=v[top]*Ten+s[i]-'0'; I++; } Top++; if(!s[i]) Break; }    returntop;}intFindint*v,intNintc) { for(inti=n-1; i>=0; i--)        if(v[i]==c)returni; return 0;}voidBuildintNint*V1,int*v2,intsum) {    if(n<=0)        return ; intP=find (v1,n,v2[n-1]); //printf ("v2[n-1]=%d p=%d\n", v2[n-1],p);sum+=v2[n-1]; if(p<=0&&n-p-1<=0) {        if(sum==min_sum) ans=min (ans,v2[n-1]); Else if(sum<min_sum) {Min_sum=sum; Ans=v2[n-1]; }        return ;    } build (P,v1,v2,sum); Build (n-p-1, v1+p+1, v2+p,sum);}intMain () { while(gets (s)) {intv;        Init (S,V1);        Gets (s); Top=init (S,V2); Ans=min_sum=10000000; Build (Top,v1,v2,0); printf ("%d\n", ans); }    return 0;}
View Code

UVA548 tree (traversal of binary trees)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.