UVa 548 - Tree
来源:互联网 发布:检票软件 编辑:程序博客网 时间:2024/05/16 19:00
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
Miguel A. Revilla
1999-01-11
#include <stdio.h>int min,leaf;void build(int *in,int *post,int i,int j,int sum){ int k; int *p=in; for(k=0;k<i;k++,p++) if(*post==*p)break; sum+=*post; if(k==0&&i==1) { if(sum<min) { leaf=(*in); min=sum; } else if(sum==min&&leaf>=*in)leaf=*in; return; } if(i-k-1)build(in+k+1,post-1,i-k-1,i-1-k,sum); if(k)build(in,post-i+k,k,k,sum);}int main(){ int i=0,j=0,in[10005],post[10005]; char c; while((scanf("%d%c",&in[i++],&c))!=EOF) { min=100000; if(c!='\n')while(((scanf("%d%c",&in[i++],&c))!=EOF)&&c!='\n'); while(((scanf("%d%c",&post[j++],&c))!=EOF)&&c!='\n'); build(&in[0],&post[j-1],i,j,0); printf("%d\n",leaf); i=0;j=0; } return 0;}
- UVa 548 Tree
- uva 548 Tree
- uva 548 - Tree
- uva 548 - Tree
- UVa 548 - Tree
- uva 548 Tree
- uva 548 - Tree
- 数据结构 uva 548-Tree
- UVA 548 - Tree
- UVa 548 - Tree
- UVa 548 - Tree
- uva-548 Tree
- UVA 548 Tree
- UVA 548 tree
- uva 548 -Tree
- uva 548 Tree
- 548 - Tree (UVa OJ)
- UVA 548 Tree
- PreSubclassWindow详细分析
- IOS应用程序第一个项目HelloWorld(一)
- 周末去寻找自己心灵的鸡汤
- Windows7体验8G内存 用上真正的8G内存: ReadyFor4GB.rar命令行版
- 如何开启伪静态和重启apache
- UVa 548 - Tree
- jquery $(document).ready() 与window.onload的区别
- 传智播客开设Unity3d游戏开发及.Net开发Android程序课程
- 从360遇上苹果说起
- 关于标准库中std::make_pair的一个问题
- poj 1410
- WEB3D之X3DOM应用
- ReadyFor4GB破解win7支持4G内存恢复到原系统
- IOS应用程序创建标题栏(二)