Tree
| Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
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.
Inputthe input file contain a description of the binary tree given as the inorder and postorder traversal sequences of th At 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 a inorder traversal of the tree and the second line WI ll contain the sequence of values associated with a postorder traversal of the tree. All values would be different, greater than zero and less than 10000. Assume that no binary tree would have more than 10000 nodes or less than 1 node.
Outputfor 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 63 1 2 5 6 7 47 8 11 3 5 16 12 188 3 11 7 16 18 12 5255255
Sample Output
13255
The second-order and sequential traversal of a binary tree is given. The smallest branch point of the smallest total length of the twig; the idea is to find this binary tree, and then the minimum value, find the smallest place wrong many times, you must first find the minimum total length, and then find the same case of the smallest minor point; The last point in the post-order traversal is actually the root node, which recursively look for the left and right tree according to this in the middle sequence traversal;
Code:
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include < Algorithm> #include <queue>using namespace std; #define SI (x) scanf ("%d", &x) #define MEM (x, y) memset (x, Y, sizeof (x)) #define PI (x) printf ("%d", x) #define P_ printf ("") const int inf=0x3f3f3f3f;typedef long long ll;const int maxn= 100010;int V1[maxn],v2[maxn];char s[maxn];int ans,minlength;struct Node{int v; Node *l,*r; Node () {l=null; R=null;}}; int init (char *s,int *v) {int k=0;for (int i=0;s[i];i++) {while (IsDigit (S[i])) v[k]=v[k]*10+s[i++]-' 0 '; k++;} for (int i=0;i<k;i++) printf ("%d", V[i]);p UTS (""); return k;} int find (int n,int *v,int t) {for (int i=n-1;i>=0;i--) if (v[i]==t) return i; return 0;} node* Build (int n,int *v1,int *v2) {Node *root;if (n<=0) return null;root=new node;root->v=v2[n-1];int p=find (N,V1, V2[n-1]); Root->l=build (P,V1,V2); Root->r=build (n-p-1,v1+p+1,v2+p); return root;} void Dfs (Node *root,int length) {if (root==null) return;length+=root->v;if (root->l==null&amP;&root->r==null) {if (minlength>length) {minlength=length; ans=root->v; } else if (minlength==length) ans=min (ans,root->v); return;} DFS (Root->l,length);d FS (root->r,length);} int main () {while (gets (s)) {mem (v1,0); mem (v2,0); init (S,V1); gets (s); int k=init (S,V2); Node *root=build (K,V1,V2), Ans=minlength=inf;dfs (root,0);p rintf ("%d\n", ans);} return 0;}
Uva-548tree (recursive traversal of binary trees)