Construct Binary Tree from Inorder and Postorder Traversal

原创 2015年07月07日 14:14:18
/**
 * 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.length != postorder.length) {
            return null;
        }
        return myBuildTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
    }
    private TreeNode myBuildTree(int[] inorder, int inBeg, int inEnd, int[] postorder, int postBeg, int postEnd) {
        if (inBeg > inEnd) {
            return null;
        }
        TreeNode root = new TreeNode(postorder[postEnd]);
        int location = findLocation(inorder, inBeg, inEnd, postorder[postEnd]);
        root.left = myBuildTree(inorder, inBeg, location - 1, postorder, postBeg, postBeg + location - inBeg - 1);
        root.right = myBuildTree(inorder, location + 1, inEnd, postorder, postEnd + location - inEnd, postEnd - 1);
        return root;
    }
    private int findLocation(int[] inorder, int begin, int end, int value) {
        for (int i = begin; i <= end; i++) {
            if (inorder[i] == value) {
                return i;
            }
        }
        return -1;
    }  
}

相关文章推荐

Construct Binary Tree from Inorder and Postorder Traversal

题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu...
  • Boyhou
  • Boyhou
  • 2013年07月28日 15:53
  • 812

Construct Binary Tree from Inorder and Postorder Traversal(medium)

【题目】             Given inorder and postorder traversal of a tree, construct the binary tree. ...
  • haihaa
  • haihaa
  • 2016年05月23日 10:35
  • 99

【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 中序和后序恢复二叉树

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

【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal

【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. 【思路】 Hint: A...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Construct Binary Tree from Inorder and Postorder Traversal
举报原因:
原因补充:

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