uva 548 tree 二叉树的各种遍历

来源:互联网 发布:辛集淘宝运营培训学校 编辑:程序博客网 时间:2024/06/03 15:58

  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 63 1 2 5 6 7 47 8 11 3 5 16 12 188 3 11 7 16 18 12 5255255

Sample Output 

13255

用递归一次性解决,递归调用时,若某一个子段不存在,递归之后一定会是le>ri,因为不存在则长度len为0,不论le是多少,

ri=le+len-1必<le。

#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<utility>//#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   num<<1,le,mid#define rson    num<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)using namespace std;const int INF =0x3f3f3f3f;const int maxn=  10010   ;//const int maxm=    ;//const int INF=    ;typedef long long ll;const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);int in[maxn],pos[maxn];int cnt=0;string s;int mini,best;void build(int posle,int posri,int inle ,int inri,int num ){    if(inle>inri)  return ;    if(posle==posri)    {        if(mini>num+pos[posri])            mini=num+pos[posri],best=pos[posri];           return ;    }    int x=pos[posri];int p;    for(int i=inle;i<=inri;i++)    {        if( in[i]==x )  {p=i;break;}    }int len1=p-inle;int len2=inri-p; build( posle,posle+len1-1,inle,p-1,num+pos[posri]       ); build(posri-len2,posri-1,p+1,inri,num+pos[posri]        );}int main(){    int x;    while( getline(cin,s))    {        mini=INF;        stringstream ss(s);         cnt=0;        while(ss>>x)        {            in[++cnt]=x;        }        getline(cin,s);        stringstream ss2(s);        cnt=0;        while(ss2>>x)        {            pos[++cnt]=x;        }        build(1,cnt,1,cnt,0);      printf("%d\n",best);    }    return 0;}


0 0