leetcode Construct Binary Tree from Inorder and Postorder Traversal

原创 2016年06月01日 16:57:27

题目链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder==null||inorder.length==0)
        {
            return null;
        }


        TreeNode root=new TreeNode(postorder[postorder.length-1]);
        build(root, inorder, 0, inorder.length-1, postorder, 0, postorder.length-1);
        return root;
    }

    public void build(TreeNode root,int[] inorder, int ins,int ine,int[] postorder,int pos,int poe)
    {
        int parentIndexInMiddle=ins;

        while(inorder[parentIndexInMiddle]!=root.val)
        {
            parentIndexInMiddle++;
        }

        int leftCount=parentIndexInMiddle-ins;



        if(leftCount>0)
        {
            int leftRootIndexInPost=pos+leftCount-1;
            root.left=new TreeNode(postorder[leftRootIndexInPost]);
            build(root.left, inorder, ins, parentIndexInMiddle-1, postorder, pos, leftRootIndexInPost);
        }

        int rightCount=ine-parentIndexInMiddle;
        if(rightCount>0)
        {
            int rightRootIndexInpost=poe-1;
            root.right=new TreeNode(postorder[rightRootIndexInpost]);
            build(root.right, inorder, parentIndexInMiddle+1, ine, postorder, poe-rightCount, poe-1);
        }
    }
}

相关文章推荐

《leetCode》:Construct Binary Tree from Inorder and Postorder Traversal

题目Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume th...

【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume t...

Leetcode: Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume...

[LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume tha...

LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers...

【LeetCode】Construct Binary Tree from (Preorder or Postorder) and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal

LeetCode Construct Binary Tree from Inorder and Postorder Traversal 思考分析过程分享

思考: 1 画图: 把二叉树和其中序和后序的序列都画出来(这个很重要,我以前有时候总是凭空想象,结果脑子一片空白) 2 把问题写下来:怎么构造一个节点? 3 观察: 到底有什么特点是可以利用来解题的呢...

Leetcode_106_Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume tha...

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树

1 解题思想这道题和105的思想,基本就是一致的,这里将会说的比较简单,之说下不同 Leetcode 105. Construct Binary Tree from Preorder and Ino...
  • MebiuW
  • MebiuW
  • 2016年09月16日 16:42
  • 313

LeetCode Construct Binary Tree from Inorder and Postorder Traversal

题目: Given inorder and postorder traversal of a tree, construct the binary tree. 题意: 给定一棵树的中序遍历和...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Construct Binary Tree from Inorder and Postorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)