UVA Tree

时间:2014-06-05 05:27:11   收藏:0   阅读:268

题目如下:

Tree 

You are to determine the value of the leaf node in a given binary treethat is the terminal node of a path of least value from the root of thebinary tree to any leaf. The value of a path is the sum of values of nodesalong that path.

Input 

The input file will contain a description of the binary tree given as theinorder and postorder traversal sequences of that tree. Your program willread two line (until end of file) from the input file. The first line willcontain the sequence of values associated with an inorder traversal of thetree and the second line will contain the sequence of values associatedwith 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 shouldoutput the value of the leaf node of a path of least value. In the case ofmultiple 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

现根据中序和后序建树,再先序遍历树,对每个叶子节点,既要保存当前的和,又要保存当前的叶子节点。

AC的代码如下:



UVA Tree,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!