树的重建与遍历



1.用递归的方法重建树后,用Dps遍历,至于为何用Dps则是因为要求出最小值,

  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
#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;
    }

}


  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值