LeetCode106—从中序与后序遍历序列构造二叉树(java版)

题目描述:

标签:树    深度优先搜索   数组

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

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

 代码:

思路分析:

总:后序遍历数组最后一个元素是根root,根据根的值将中序遍历以根为分割点,分为左子树和右子树,再以左子树和右子树的个数分割后序遍历数组,也分为左子树和右子树,再将中序左子树和后序左子树为一组递归,中序右子树和后序右子树为一组递归。

1、递归结束条件,如果后序遍历数组长度为0,说明为空,返回null;或者取后序遍历数组的最后一个元素,rootValue,构造根节点,若后序遍历数组长度为1,说明仅有1个结点,返回该结点

2、循环中序遍历数组,找到分割点delimiterIndex

3、分割中序遍历数组,都是左闭右开区间。左子树的leftInorderBegin=inorderBegin,leftInorderEnd=delimiterIndex;右子树的rightInorderBegin=delimiterIndex+1,rightInorderEnd=inorderEnd

4、分割后序遍历数组,都是左闭右开区间。左子树的leftPostorderBegin=postorderBegin,leftPostorderEnd= postorderBegin + delimiterIndex - inorderBegin;

                                                                      右子树的rightPostorderBegin=postorderBegin + delimiterIndex - inorderBegin,rightInorderEnd=postorderEnd - 1;

5、root.left=遍历(中序左子树,后序左子树)

     root.right=遍历(中序右子树,后序右子树)

6、返回根节点root(一般是针对一个根节点构造完他的左子结点和右子结点后,将它返回作为别的根节点的左/右子结点)

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

    public TreeNode traversal(int[] inorder,int inorderBegin,int inorderEnd,int[] postorder,int postorderBegin,int postorderEnd){
        //右子树遍历数组为空
        if(postorderBegin == postorderEnd){
            return null;
        }
        //后序遍历数组的最后一个节点即为根节点
        int rootValue = postorder[postorderEnd - 1];
        TreeNode root = new TreeNode(rootValue);
        //叶子结点
        if(postorderEnd - postorderBegin == 1){
            return root;
        }
        //找到中序遍历数组中的切割位置
        int delimiterIndex;
        for(delimiterIndex = inorderBegin;delimiterIndex < inorderEnd;delimiterIndex++){
            if(inorder[delimiterIndex] == rootValue) break;
        }
        //切割中序遍历数组为左子树和右子树
        int leftInorderBgein = inorderBegin;
        int leftInorderEnd = delimiterIndex;
        int rightInorderBegin = delimiterIndex + 1;
        int rightInorderEnd = inorderEnd;
        //切割后序遍历数组为左子树和右子树
        int leftPostorderBegin = postorderBegin;
        int leftPostorderEnd = postorderBegin + (delimiterIndex - inorderBegin);
        int rightPostorderBegin = postorderBegin + (delimiterIndex - inorderBegin);
        int rightPostorderEnd = postorderEnd - 1;

        root.left = traversal(inorder,leftInorderBgein,leftInorderEnd,postorder,leftPostorderBegin,leftPostorderEnd);
        root.right = traversal(inorder,rightInorderBegin,rightInorderEnd,postorder,rightPostorderBegin,rightPostorderEnd);

        return root;
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值