UVA 548 Tree(数据结构,二叉树,遍历)
来源:互联网 发布:淘宝上的进口零食推荐 编辑:程序博客网 时间:2024/06/03 15:58
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489
Tree
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
给出二叉树的中根遍历和后根遍历,找出根节点到叶子节点权值和最小的路径,输出该叶子节点
建树+DFS
建树的过程完全考察对二叉树遍历及递归的理解,下面给出递归定义的二叉树T的先/中/后根遍历:
先根:T的根节点+T的左子树+T的右子树
中根:T的左子树+T的根节点+T的右子树
后根:T的左子树+T的右子树+T的根节点
已知中根和另一个遍历就可确定一棵树(确定根节点,分出左右子树)
多手动模拟几次建树就能明白过程了,对基本功要求比较高
DFS可以加个剪枝,不过似乎没什么效果
#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cctype>#include<cmath>#include<string>#include<cstring>#include<stack>#include<queue>#include<list>#include<vector>#include<map>#include<set>#define sqr(x) ((x)*(x))#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#define mm 10001using namespace std;int InOrder[mm],PostOrder[mm];struct Node{ int v,l,r;}node[mm];int n,cnt,MIN,ANS;int build(int Il,int Ir,int Pl,int Pr){ //printf("Il=%d Ir=%d Pl=%d Pr=%d:\n",Il,Ir,Pl,Pr); int k; if (Il==Ir) return -1; for (int i=Il;i<Ir;i++) { if (InOrder[i]==PostOrder[Pr-1]) { k=i; break; } } int t=cnt++; node[t].v=InOrder[k]; node[t].l=build(Il,k,Pl,Pl+k-Il); node[t].r=build(k+1,Ir,Pr-Ir+k,Pr-1); return t;}void dfs(int x,int s){ if (s>MIN) return ; if (node[x].l==-1 && node[x].r==-1) { if (s+node[x].v<MIN) { MIN=s+node[x].v; ANS=node[x].v; return ; } } if (node[x].l!=-1) dfs(node[x].l,s+node[x].v); if (node[x].r!=-1) dfs(node[x].r,s+node[x].v);}int main(){ #ifndef ONLINE_JUDGE freopen("/home/fcbruce/文档/code/t","r",stdin); #endif // ONLINE_JUDGE char c; while (scanf("%d%c",InOrder+0,&c)==2) { n=1; while (true) { if (c=='\n') break; scanf("%d%c",InOrder+n,&c); n++; } //printf("%d\n",n); for (int i=0;i<n;i++) scanf("%d%c",PostOrder+i,&c); cnt=0; build(0,n,0,n); MIN=INF; dfs(0,0); printf("%d\n",ANS); } return 0;}
1 0
- UVA 548 Tree(数据结构,二叉树,遍历)
- 【UVA 548 Tree】二叉树构造 & 遍历
- UVa 548 - Tree 二叉树的重建与遍历
- UVa 548 - Tree【二叉树的递归遍历(DFS)】
- uva 548 tree 二叉树的各种遍历
- Uva 248 Tree//二叉树遍历,递归
- 二叉树 UVA 548 Tree
- UVA - 548 Tree 二叉树
- UVa--548 Tree(二叉树)
- UVA 536 Tree Recovery (二叉树的遍历)
- 数据结构之二叉树的遍历 HDU1710 Binary Tree Traversals
- UVA 112 & POJ 1145 Tree Summing(数据结构,二叉树,栈)
- 数据结构 uva 548-Tree
- UVa 548 - Tree 二叉树的重建——中序遍历与后续遍历进行建树
- 二叉树遍历 - 数据结构
- 数据结构 二叉树遍历
- 数据结构 - 二叉树遍历
- 二叉树遍历 - 数据结构
- Linux基本命令大全
- linux+mysql+tomcat+jdk
- mysql帮助的使用
- 设计模式---工厂模式
- Unity3D开发:为地形添加水源和效果
- UVA 548 Tree(数据结构,二叉树,遍历)
- 使用live delegate on来解决js对后加载的html失效的问题
- Adding a library/JAR to an Eclipse Android project
- jquery ajax 在submit按钮的click处理中应注意的地方
- 怎样跟他人建立好人际关系
- 关于IIS发布应用的几点总结
- HTML5 localStorage and sessionStorage
- 配置mac 环境时碰到的一些问题
- 【Unknown】HttpClient:一个HTTP库规范