UVA 548 Tree(数据结构,二叉树,遍历)
2014-05-11 01:45
477 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489
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.
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.
node.
Miguel A. Revilla
1999-01-11
给出二叉树的中根遍历和后根遍历,找出根节点到叶子节点权值和最小的路径,输出该叶子节点
建树+DFS
建树的过程完全考察对二叉树遍历及递归的理解,下面给出递归定义的二叉树T的先/中/后根遍历:
先根:T的根节点+T的左子树+T的右子树
中根:T的左子树+T的根节点+T的右子树
后根:T的左子树+T的右子树+T的根节点
已知中根和另一个遍历就可确定一棵树(确定根节点,分出左右子树)
多手动模拟几次建树就能明白过程了,对基本功要求比较高
DFS可以加个剪枝,不过似乎没什么效果
Tree |
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 inputfile. 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 terminalnode.
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
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 10001 using 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; }
相关文章推荐
- UVa 548 - Tree 二叉树的重建与遍历
- 【UVA 548 Tree】二叉树构造 & 遍历
- UVA548 Tree (二叉树的遍历)
- UVa 548 (二叉树的递归遍历) Tree
- uva548--tree(二叉树的递归遍历)
- UVa 548 - Tree【二叉树的递归遍历(DFS)】
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
- 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(二叉树的建立)
- UVA548 Tree(DFS,二叉树)
- 【数据结构_树_Tree_0983】利用二叉树中序和后序遍历确定二叉树的先序遍历
- UVA - 548 Tree 二叉树
- UVa--548 Tree(二叉树)