从前序与中序遍历序列构造二叉树&&从后序与中序遍历序列构造二叉树

39 篇文章 0 订阅

根据一棵树的前序遍历与中序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:

在这里插入图片描述

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

在这里插入图片描述

首先设置一个变量leftCount为下标,前序第一个为根节点,之后再中序中寻找根节点,记录它的下标为leftCount 。然后在前序遍历序列和中序遍历序列中分别找到根节点之后,左子树的前序遍历是从下标1开始,到leftCount,左子树的中序遍历是从下标0开始,到leftCount-1。右子树的前序遍历是是从1+leftCount到最后一个元素,右子树的中序遍历是从leftCount+1到最后一个元素。
之后分别使用递归构造左子树和右子树,递归出口为**任意一个序列为空,**表示构造完成,这个时候就返回空。

class Solution {
     public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder.length == 0) {
            return null;
        }
        int rootValue = preorder[0];
        int leftCount;
        for (leftCount = 0; leftCount < inorder.length; leftCount++) {
            if (inorder[leftCount] == rootValue) {
                break;
            }
        }
        TreeNode root = new TreeNode(rootValue);                    **//注意:Arrays.copyOfRange()方法是左开右闭区间**
        int[] leftPreorder = Arrays.copyOfRange(preorder,
                1, 1 + leftCount);
        int[] leftInorder = Arrays.copyOfRange(inorder, 0, leftCount);
        root.left = buildTree(leftPreorder, leftInorder);
        int[] rightPreorder = Arrays.copyOfRange(preorder,
                1 + leftCount, preorder.length);
        int[] rightInorder = Arrays.copyOfRange(inorder,
                leftCount + 1, inorder.length);
        root.right = buildTree(rightPreorder, rightInorder);

        return root;
    }
}

根据一棵树的中序遍历与后序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:

在这里插入图片描述

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
在这里插入图片描述
和上一个不同的是跟是最后一个元素同样的,在中序遍历里找根节点,下标用一个变量leftCount 来表示,左子树的后序遍历和中序遍历序列都为从下标为0的开始,到下标为leftCount-1的元素,右子树的后序遍历序列为
leftCount为下标的元素开始到倒数第二个元素,中序遍历为下标为leftCount 的元素到最后一个元素。

class Solution {
          public TreeNode buildTree(int[] postorder, int[] inorder) {
            if (postorder.length == 0) {
                return null;
            }
            int rootValue = postorder[postorder.length-1];
            int leftCount;
            for (leftCount = 0; leftCount < inorder.length; leftCount++) {
                if (inorder[leftCount] == rootValue) {
                    break;
                }
            }
            TreeNode root = new TreeNode(rootValue);
            int[] leftPostorder = Arrays.copyOfRange(postorder,
                    0, leftCount);
            int[] leftInorder = Arrays.copyOfRange(inorder, 0, leftCount);
            root.left =buildTree(leftPostorder, leftInorder);
            int[] rightPostorder = Arrays.copyOfRange(postorder,
                    leftCount, postorder.length-1);
            int[] rightInorder = Arrays.copyOfRange(inorder,
                    leftCount + 1, inorder.length);
            root.right =buildTree(rightPostorder, rightInorder);

            return root;
        }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值