1.用递归的方法重建树后,用Dps遍历,至于为何用Dps则是因为要求出最小值,
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.
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.
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.
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
1
3
255
#include <iostream>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<sstream>
#include<cctype>
#include<queue>
#include <cstring>
#include <cstdlib>
#include <fstream>
using namespace std;
struct NodeT{
int value;
NodeT *left,*right;
};
int inor_int[10005],post_int[10005],temp,inor_cnt,post_cnt,sum,min_value,min_leaf;
NodeT *empty(){
NodeT *p=new NodeT;
p->left=NULL;
p->right=NULL;
return p;
}
NodeT *build(int *in_,int *post_,int n){
int mi_index;
if(n<=0){return NULL;}
for(int i=0;i<n;i++){if(post_[n-1]==in_[i]){mi_index=i;break;}}
//前面有mi_index个,后面有n-1-mi_index个。
NodeT *l,*r,*root;
l=build(in_,post_,mi_index);
r=build(in_+(mi_index+1),post_+mi_index,n-1-mi_index);
root=empty();
root->value=post_[n-1];
root->left=l;
root->right=r;
return root;
}
void dps(NodeT *root){
bool flag=false;
if(root->left){sum+=root->value;dps(root->left);sum-=root->value;flag=true;}
if(root->right){sum+=root->value;dps(root->right);sum-=root->value;flag=true;}
if(flag==false){
if((sum+root->value)<min_value){
min_value=sum+root->value;
min_leaf=root->value;
}
else if((sum+root->value)==min_value){
if(root->value<min_leaf)min_leaf=root->value;
}
}
}
int main()
{
ifstream cin("ha.txt");
char inor[1000000],post[1000000],*p;
NodeT *root;
while(cin.getline(inor,1000000)){
cin.getline(post,1000000);
p=inor;inor_cnt=0;
while(*p){
//把inor_int 转化
if(*p>='0'&&*p<='9'){
temp=0;
while(*p>='0'&&*p<='9'){temp=temp*10+*p-'0';p++;}
inor_int[inor_cnt++]=temp;
}
else{p++;}
}
p=post;post_cnt=0;
while(*p){
//把inor_int 转化
if(*p>='0'&&*p<='9'){
temp=0;
while(*p>='0'&&*p<='9'){temp=temp*10+*p-'0';p++;}
post_int[post_cnt++]=temp;
}
else{p++;}
}
root=build(inor_int,post_int,post_cnt);
sum=0;
min_value=999999999;min_leaf=9999999;
dps(root);
cout<<min_leaf<<endl;
}
}
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.
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.
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.
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
1
3
255