【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】

【106-Construct Binary Tree from Inorder and Postorder 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.

题目大意

  给定一个中序遍历和后序遍历序列,构造一棵二叉树

  注意:

  树中没有重复元素

解题思路

  后序遍历的最后一个元素就是树的根结点(值为r),在中序遍历的序列中找值为r的位置idx,idx将中序遍历序列分为左右两个子树,对应可以将后序遍历的序列分在两个子树,递归对其进行求解。

代码实现

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 || postorder == null || inorder.length == 0
                || inorder.length != postorder.length) {
            return null;
        }

        // 构建二叉树
        return solve(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
    }

    /**
     * 构建二叉树
     *
     * @param inorder   中序遍历的结果
     * @param x         中序遍历的开始位置
     * @param y         中序遍历的结束位置
     * @param postorder 后序遍历的结果
     * @param i         后序遍历的开始位置
     * @param j         后序遍历的结束位置
     * @return 二叉树
     */
    public TreeNode solve(int[] inorder, int x, int y, int[] postorder, int i, int j) {

        if (x >= 0 && x <= y && i >= 0 && i <= j) {
            // 只有一个元素,(此时也有i=j成)
            if (x == y) {
                return new TreeNode(postorder[j]);
            }
            // 多于一个元素,此时也有i<j
            else if (x < y) {
                // 创建根结点
                TreeNode root = new TreeNode(postorder[j]);

                // 找根结点在中序遍历的下标
                int idx = x;
                while (idx < y && inorder[idx] != postorder[j]) {
                    idx++;
                }

                // 左子树非空,构建左子树
                int leftLength = idx - x;
                if (leftLength > 0) {
                    // i, i + leftLength - 1,前序遍历的左子树的起始,结束位置
                    root.left = solve(inorder, x, idx - 1, postorder, i, i + leftLength - 1);
                }

                // 右子树非空,构建右子树
                int rightLength = y - idx;
                if (rightLength > 0) {
                    // i + leftLength, j - 1,前序遍历的右子树的起始,结束位置
                    root.right = solve(inorder, idx + 1, y, postorder, i + leftLength, j - 1);
                }

                return root;
            } else {
                return null;
            }
        }

        return null;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47371993

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-16 19:48:32

【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】的相关文章

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

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

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 that duplicates do not exist in the tree. 题目标签:Array, Tree 这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order.因为post-order是最后一个数字是root,所以要从右向左的遍历.还需要把helpe

【LeetCode】105 &amp; 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 that duplicates do not exist in the tree. 提示: 题目要求通过一颗二叉树的中序遍历及后续遍历的结果,将这颗二叉树构建出来,另外还有一个已知条件,所有节点的值都是不同的. 首先需要了解一下二叉树不同遍历方式的定义: 前序遍历:首先访问根结点,然后遍历左子树,最

C#解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 that duplicates do not exist in the tree. 这个题目是给你一棵树的中序遍历和后序遍历,让你将这棵树表示出来.其中可以假设在树中没有重复的元素. 当做完这个题之后,建议去做做第105题,跟这道题类似. 分析:这个解法的基本思想是:我们有两个数组,分别是IN和POST.后

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 that duplicates do not exist in the tree. 思路:这题和上题类似,前序第一个是根节点,后序遍历最后一个是根节点.其余步骤类似. 代码如下: /** * Definition for a binary tree node. * public class TreeNod

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 / 9 20

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 that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 /

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 that duplicates do not exist in the tree. ======== 利用:中序+后序遍历 ==== code: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod

106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {