zoukankan      html  css  js  c++  java
  • lintcode: 中序遍历和后序遍历树构造二叉树

    题目

    中序遍历和后序遍历树构造二叉树

    根据中序遍历和后序遍历树构造二叉树

    样例

    给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2]

    返回如下的树:

      2

     /  

    1    3

    注意

    你可以假设树中不存在相同数值的节点

    解题

    1.后序遍历最后一个结点就是根节点,根据这个根结点把中序遍历划分开来,同时也把后续遍历划分开来

    2.递归就好了

    程序感觉很简单不知道怎么写的,程序来源于九章

    /**
     * Definition of TreeNode:
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left, right;
     *     public TreeNode(int val) {
     *         this.val = val;
     *         this.left = this.right = null;
     *     }
     * }
     */
     
     
    public class Solution {
        /**
         *@param inorder : A list of integers that inorder traversal of a tree
         *@param postorder : A list of integers that postorder traversal of a tree
         *@return : Root of a tree
         */
          private int findPosition(int[] arr, int start, int end, int key) {
            int i;
            for (i = start; i <= end; i++) {
                if (arr[i] == key) {
                    return i;
                }
            }
            return -1;
        }
    
        private TreeNode myBuildTree(int[] inorder, int instart, int inend,
                int[] postorder, int poststart, int postend) {
            if (instart > inend) {
                return null;
            }
    
            TreeNode root = new TreeNode(postorder[postend]);
            int position = findPosition(inorder, instart, inend, postorder[postend]);
    
            root.left = myBuildTree(inorder, instart, position - 1,
                    postorder, poststart, poststart + position - instart - 1);
            root.right = myBuildTree(inorder, position + 1, inend,
                    postorder, poststart + position - instart, postend - 1);
            return root;
        }
    
        public TreeNode buildTree(int[] inorder, int[] postorder) {
            if (inorder.length != postorder.length) {
                return null;
            }
            return myBuildTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
        }
    }
    Java Code

    自己又实现了一遍,并加了注释

    /**
     * Definition of TreeNode:
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left, right;
     *     public TreeNode(int val) {
     *         this.val = val;
     *         this.left = this.right = null;
     *     }
     * }
     */
     
     
    public class Solution {
        /**
         *@param inorder : A list of integers that inorder traversal of a tree
         *@param postorder : A list of integers that postorder traversal of a tree
         *@return : Root of a tree
         */
        public TreeNode buildTree(int[] inorder, int[] postorder) {
            // write your code here
            if(inorder.length != postorder.length)
                return null;
            return buildTree(inorder,0,inorder.length -1 ,postorder,0,postorder.length -1 );
        }
        public int findroot(int[] inorder,int r){
            for(int i=0;i<inorder.length;i++)
                if(inorder[i] == r)
                    return i;
            return -1;
        }
        public TreeNode buildTree(int[] inorder ,int istart,int iend,int[] postorder,int pstart,int pend){
            if(istart > iend)
                return null;
            int r = postorder[pend];
            // 跟结点
            TreeNode root = new TreeNode(r);
            // 找到根节点
            int l = findroot(inorder,r);
            // 左子树 中序遍历 起始结束位置以此是:istart l-1 
            //后序遍历 起始位置是:pstart 结束位置:pstart(已经占据了一个位置所以要-1) + (左子树的长度) - 1
            root.left = buildTree(inorder,istart,l-1,postorder,pstart,pstart+(l-1 - istart + 1) -1);
            // 右子树 中序遍历 起始结束位置:l+1 iend
            // 后序遍历 起始位置:pstart + (左子树的长度) ,结束位置 pend -1
            root.right = buildTree(inorder,l+1,iend,postorder,pstart + (l-1-istart+1),pend -1);
            return root;
        }
    }
  • 相关阅读:
    我的WCF之旅(1):创建一个简单的WCF程序
    网页设计中颜色的搭配
    CSS HACK:全面兼容IE6/IE7/IE8/FF的CSS HACK
    UVa 1326 Jurassic Remains
    UVa 10340 All in All
    UVa 673 Parentheses Balance
    UVa 442 Matrix Chain Multiplication
    UVa 10970 Big Chocolate
    UVa 679 Dropping Balls
    UVa 133 The Dole Queue
  • 原文地址:https://www.cnblogs.com/theskulls/p/5127410.html
Copyright © 2011-2022 走看看