题意 : 建立一颗树然后计算每一条到叶子节点路径的中值和最小的,输出叶子节点值
坑爹一个最后del()多写一个一直RERERE。。
题目:
Tree |
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
代码:
1 #include <iostream> 2 #include <string> 3 #include <sstream> 4 #include <limits.h> 5 #include <malloc.h> 6 #include <cstdio> 7 #include <string.h> 8 using namespace std; 9 10 const int maxn = 50000; 11 12 typedef struct Tnode 13 { 14 int data; 15 struct Tnode* left,*right; 16 }Node; 17 18 int inorder[maxn]; 19 int postorder[maxn]; 20 int pi,pp; 21 22 int num = 0; 23 24 Node* newnode() 25 { 26 Node* T; 27 T=(Node*)malloc(sizeof(Node)); 28 if(!T)return NULL; 29 T->left=NULL;T->right=NULL; 30 T->data=0; 31 return T; 32 } 33 34 35 Node* build(int left,int right,int* inorder) 36 { 37 //cout<<"LEfT : "<<left<<"RIGHT : "<<right<<endl; 38 39 //cout<<"POST :"<<pp<<" "<<postorder[pp]<<" NExt"<<pp-1<<" "<<postorder[pp-1]<<endl; 40 //cout<<endl; 41 42 43 if(left>right||left<0 || right>=pi)return NULL; 44 if(left==right) 45 { 46 //cout<<"here1"<<endl; 47 Node* now; 48 now = newnode(); 49 now->data=inorder[left]; 50 now->left=now->right=NULL; 51 pp--; 52 return now; 53 } 54 55 56 for(int i=left;i<=right;i++) 57 { 58 if(inorder[i]==postorder[pp-1]) 59 { 60 //cout<<"here2"<<endl; 61 Node* now; 62 now = newnode();now->data= inorder[i]; 63 pp--; 64 now->left = build(i+1,right,inorder); 65 // if(!now->left)pp--; 66 now->right = build (left,i-1,inorder); 67 return now; 68 } 69 } 70 71 } 72 void print(Node* T) 73 { 74 if(T) 75 { 76 cout<<T->data<<" "; 77 print(T->left); 78 print(T->right); 79 } 80 } 81 int MIN,LAST,ans; 82 void cal(int sum,Node* T) 83 { 84 if(T==NULL)return; 85 sum+=T->data; 86 if(T->left==NULL && T->right==NULL) 87 { 88 if(sum<MIN) 89 { 90 MIN=sum; 91 LAST= T->data; 92 ans=LAST; 93 } 94 if(sum==MIN && T->data<LAST) 95 { 96 LAST=T->data; 97 ans=LAST; 98 } 99 } 100 else 101 { 102 if(T->left) 103 cal(sum,T->left); 104 if(T->right) 105 cal(sum,T->right); 106 } 107 108 } 109 110 111 void del(Node* T) 112 { 113 if(T) 114 { 115 del(T->left); 116 del(T->right); 117 free(T); 118 } 119 } 120 Node* root; 121 int main() 122 { 123 int ti; 124 char tc; 125 while(scanf("%d%c",&ti,&tc)!=EOF) 126 { 127 inorder[pi++]=ti; 128 while(tc!=' ') 129 { 130 scanf("%d%c",&ti,&tc); 131 inorder[pi++]=ti; 132 } 133 scanf("%d%c",&ti,&tc); 134 postorder[pp++]=ti; 135 while(tc!=' ') 136 { 137 scanf("%d%c",&ti,&tc); 138 postorder[pp++]=ti; 139 } 140 141 root=build(0,pi-1,inorder); 142 // print(root); 143 // cout<<endl; 144 MIN = INT_MAX; 145 LAST = 0; 146 ans=0; 147 cal(0,root); 148 cout<<ans<<endl; 149 del(root); 150 pi=0;pp=0; 151 memset(inorder,0,sizeof(inorder)); 152 memset(postorder,0,sizeof(postorder)); 153 //print(); 154 } 155 //del(root); 156 return 0; 157 }