uva548Tree
来源:互联网 发布:环球天下期货软件 编辑:程序博客网 时间:2024/06/06 14:56
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
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
题目大意:
给你一棵二叉树的中序遍历和后序遍历,让你找从根到叶子结点的路径最短的叶子结点,如果有多个路径符合,就选择叶子结点最小的结点。
然而题意看错两次。。。。。。第一次以为只找值最小的叶子结点,第二次以为选择第一次出现的最短路径。。。。。。注意以后看题时不要通过input,output来推测题意。而是在把前言理解的基础上,再看题。题目错了就呵呵了。
思路:
因为题目只要求解出结果,便不需要实际建树,只是模拟建树即可,用两个变量p1,p2限制后序遍历的树的左右界限,in1,in2来限制中序遍历树的左右界限,还有一个变量path保存路过结点的值。将一个大树劈成两个左右子树(更新path),,,,,,一直给我劈,,,,,,直到只剩一个枝条没法劈,才将它与最短路径比较。
#include<iostream>#include<cstring>#include<stdio.h>#include<sstream>using namespace std;const int inf = 0x3f3f3f3f;int inorder[10004], n, ans, minn;int postorder[10004];void build(int pl, int pr, int inl, int inr, int path){if (pl > pr)return;if (pl == pr){if (path + postorder[pl] <= minn){if (path + postorder[pl] == minn){ans = ans < postorder[pl] ? ans : postorder[pl];}else{ minn = path + postorder[pl]; ans = postorder[pl];}}return;}int fir = postorder[pr];for (int i = inl; i <= inr; ++i){if (inorder[i] == fir){build(pl, pl + i - inl - 1, inl, i - 1, path + inorder[i]);build(pr - (inr - i), pr - 1, i + 1, inr, path + inorder[i]);break;}}}int main(){memset(inorder, 0, sizeof(inorder));memset(postorder, 0, sizeof(postorder));string s;while (getline(cin, s)){if (s.size() == 0)break;stringstream st(s);n = -1;while (st >> inorder[++n]);getline(cin, s);stringstream str(s);int cn = 0;ans = inf;minn = inf;while (str >> postorder[cn++]);build(0, n - 1, 0, n - 1, 0);cout << ans << endl;}}
0 0
- uva548Tree
- UVA548Tree
- UVA548Tree
- 二叉树--uva548Tree 二叉树递归遍历
- 数组一直报下标溢出,通过null判断都没用
- Android LED控制
- 如何通过Maven的Jetty插件运行Web工程
- 线程池的使用(二)
- 内存泄漏的学习
- uva548Tree
- Linux下Tomcat重新启动
- apache fluent 乱码
- C#中UDP数据的发送、接收
- 机器学习的一般算法
- Java程序通过LDAP对用户进行登陆验证
- Sql Server 数据类型与 C# 数据类型对照
- ios开发学习笔记--数据持久化之数据库(SQLite.swift)和FMDB
- 与Service通信的简单总结