【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 that duplicates do not exist in the tree.

【解析】

题意:根据二叉树中序遍历和后序遍历的结果,构造该二叉树。

首先明确一下,中序遍历顺序:left - root - right,后序遍历顺序:left  - right - root。

很显然,后序遍历的最后一个节点就是该二叉树的根节点。

找到根节点在中序遍历数组中的位置,那么其之前的元素都是根节点的左支树,其之后的元素都是根节点的右支数。

这样递归左右两个子数组,分别找出左右子树的根节点。

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

    // sub-tree ranges from begin to end in inorder[], it's root is postorder[rootpos]
    public TreeNode getRoot(int begin, int end, int rootpos) {
        if (begin > end) return null;
        TreeNode root = new TreeNode(postorder[rootpos]);
        int i;
        for (i = begin; i <= end; i++) {
            if (inorder[i] == postorder[rootpos]) break;
        }
        // left sub-tree has (i - begin) nodes, right sub-tree has (end - i) nodes
        root.left = getRoot(begin, i - 1, rootpos - 1 - (end - i));
        root.right = getRoot(i + 1, end, rootpos - 1);
        return root;
    }
}
时间: 2024-08-05 05:09:35

【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal 解题报告的相关文章

LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search SOLUTION 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 that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

[leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是roo

[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 that duplicates do not exist in the tree. 这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见我之前的一篇博客Convert Sorted Array to Bin

[Leetcode] Construct Binary Tree from Inorder and Postorder Traversal I,II

这两个问题实际上是同一个问题,需要对三种遍历方式的规律非常清楚. 对于前序遍历,第一个元素实际上就是root,然后后面的元素前半部分是左树的node,后半部分是右树的node 对于中序遍历,一旦我们知道了root节点,那么就可以将其分为两半部分,也就是左树和右树 对于后序遍历,我们可以缺点最后一个节点是root节点,剩下的部分:前半部分是左树节点,后半部分是右树节点. 这样使用递归就可以解决,需要使用两组index划定两种遍历的range,当left>right的时候,就需要返回null 有点类

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode java

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 题解: 这道题跟pre+in一样的方法做,只不过找左子树右子树的位置不同而已. 1 / \ 2 3 / \ / \ 4 5 6 7 对于上图的树来说, index: 0 1 2 3 4 5 6 中序遍历为: 4 2

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr