UVA 546 Tree (二叉树综合运用)

来源:互联网 发布:golang开发是什么 编辑:程序博客网 时间:2024/06/06 15:46

https://vjudge.net/problem/UVA-548


You are to determine the value of the leaf node in a given binary tree that is the terminal node of apath of least value from the root of the binary tree to any leaf. The value of a path is the sum of valuesof nodes along that path.


Input

The input file will contain a description of the binary tree given as the inorder and postorder traversalsequences of that tree. Your program will read two line (until end of file) from the input file. The firstline will contain the sequence of values associated with an inorder traversal of the tree and the secondline will contain the sequence of values associated with a postorder traversal of the tree. All valueswill be different, greater than zero and less than 10000. You may assume that no binary tree will havemore 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 thecase 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


思路:这道题分为两个部分:

第一部分是根据二叉树的中序遍历和后序遍历建立二叉树。

第二部分是根据建立好的二叉树运用DFS搜索出从根节点到叶子结点的节点之和最小的路径,并输出路径的叶子结点。

建树的过程很简单,根据后序遍历的性质,即从末尾向前遇到的节点为根节点,然后再中序遍历中找到根节点对应的下标i,然后递归的调用建树过程即可,值得注意的是,因为后序遍历是线性查找,因此可以用全局变量index,只需动态维护一个index即可,具体实现细节可看代码

关键的是搜索的过程,首先用全局变量sum记录每条路径中的最小值,singSum记录单条路径的值,ans记录最短路径对应的叶子结点的值。

PS:递归结束后注意对singSum变量进行回溯处理。

import java.util.Scanner;import java.util.Vector;public class Main {private static int index;private static int sum,ans,singSum;public static void main(String[] args) {Scanner scan = new Scanner(System.in);while(scan.hasNext()){String a = scan.nextLine();String b = scan.nextLine();String[] aa = a.split(" ");String[] bb = b.split(" ");Vector<Integer> inorder = new Vector<Integer>();Vector<Integer> postorder = new Vector<Integer>();for(int i=0;i<aa.length;i++){inorder.add(Integer.valueOf(aa[i]));}for(int i=0;i<bb.length;i++){postorder.add(Integer.valueOf(bb[i]));}index = inorder.size()-1;Node root = buildTree(inorder,postorder,0,index);sum = Integer.MAX_VALUE;//所有路径最小值ans = 0;//最短路径的叶子结点的权值singSum = 0;//单挑路径和dfs(root);System.out.println(ans);//输出结果}}//搜索public static void dfs(Node root){if(root!=null){//System.out.print(root.v+" ");if(root.left==null&&root.right==null){singSum+=root.v;//System.out.println(singSum+",v=="+root.v);if(sum>singSum){sum = singSum;ans = root.v;}singSum-=root.v;}if(root.left!=null){singSum+=root.v;//递归dfs(root.left);singSum-=root.v;//回溯}if(root.right!=null){singSum+=root.v;//递归dfs(root.right);singSum-=root.v;//回溯}}}//递归建树public static Node buildTree(Vector<Integer> inorder,Vector<Integer> postorder,int start,int end){int v = postorder.get(index);int i = start;for(;i<=end;i++){if(inorder.get(i)==v)break;}Node root = new Node(v);//注意:先右子树,后左子树if(i+1<=end){index--;//根节点下表减1root.right = buildTree(inorder,postorder,i+1,end);}if(i-1>=start){index--;//根节点下表减1root.left = buildTree(inorder,postorder,start,i-1);}return root;}//节点static class Node{int v;Node left,right;public Node(int v){this.v = v;left = null;right = null;}}}



0 0