递归构造二叉树

根据中序遍历和后序遍历构造二叉树
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
根据上述两个遍历数组,可以构造出如下的二叉树
在这里插入图片描述
构造过程就是每次找到后序遍历数组的最后的一个值,就是根节点,然后在中序遍历中找到该节点所对应的下标,将中序数组分割为两个数组,再到后序数组中找到对应的两个数组,递归构造。
在这里插入图片描述
具体步骤如下:
第一步:如果数组大小为零的话,说明是空节点了。
第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
第五步:切割后序数组,切成后序左数组和后序右数组(确定边界时可根据对应中序数组的大小)
第六步:递归处理左区间和右区间
具体代码如下:

public TreeNode buildTree(int[] inorder, int[] postorder) {
        int m = inorder.length, n = postorder.length;
        if (m == 0 || n == 0 || m != n) return null;
        return dfs(inorder, 0, m, postorder, 0, n);
    }
    public TreeNode dfs(int[] inorder, int inorderBegin, int inorderEnd, int[] postorder, int postorderBegin, int postorderEnd) {
        //后序为空,则直接返回
        if (postorderEnd == postorderBegin) return null;
        //若不为空,构造一个后序数组最后一个数的节点
        int rootVal = postorder[postorderEnd - 1];
        TreeNode root = new TreeNode(rootVal);
        //后序数组大小为1,则返回该叶子节点
        if (postorderEnd - postorderBegin == 1) return root;
        //找切割点,采用左闭右开区间
        int delimiterIndex;
        for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {
            if (inorder[delimiterIndex] == rootVal) {
                break;
            }
        }
        //切割中序数组,得到中序左数组和中序右数组
        int inorderLeftBegin = inorderBegin;
        int inorderLeftEnd = delimiterIndex;
        int inorderRightBegin = delimiterIndex + 1;
        int inorderRightEnd = inorderEnd;
        //切割后序数组,得到后序左数组和后序右数组(可利用数组大小相等)
        int postorderLeftBegin = postorderBegin;
        int postorderLeftEnd = postorderBegin + delimiterIndex - inorderLeftBegin;
        int postorderRightBegin = postorderLeftEnd;
        int postorderRightEnd = postorderEnd - 1;
        //递归
        root.left = dfs(inorder, inorderLeftBegin, inorderLeftEnd, postorder, postorderLeftBegin,postorderLeftEnd);
        root.right = dfs(inorder, inorderRightBegin, inorderRightEnd, postorder, postorderRightBegin,postorderRightEnd);
        return root;
    }

根据前序遍历和中序遍历构造二叉树
与上述相似,只是每次取前序数组的第一个数作为根节点

public TreeNode buildTree(int[] preorder, int[] inorder) {
        int m = preorder.length, n = inorder.length;
        if (m == 0 || n == 0 || m != n) return null;
        return dfs(preorder, 0, m, inorder, 0, n);
    }
    public TreeNode dfs(int[] preorder, int preorderBegin, int preorderEnd, int[] inorder, int inorderBegin, int inorderEnd) {
        if (preorderBegin == preorderEnd) return null;
        int rootVal = preorder[preorderBegin];
        TreeNode root = new TreeNode(rootVal);
        if (preorderEnd - preorderBegin == 1) return root;
        int delimiterIndex;
        for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {
            if (inorder[delimiterIndex] == rootVal) break;
        } 
        //分割中序数组
        int inorderLeftBegin = inorderBegin;
        int inorderLeftEnd = delimiterIndex;
        int inorderRightBegin = delimiterIndex + 1;
        int inorderRightEnd = inorderEnd;
        //分割前序数组
        int preorderLeftBegin = preorderBegin + 1;
        int preorderLeftEnd = preorderLeftBegin + inorderLeftEnd - inorderLeftBegin;
        int preorderRightBegin = preorderLeftEnd;
        int preorderRightEnd = preorderEnd;
        //构造左右子树递归
        root.left = dfs(preorder, preorderLeftBegin, preorderLeftEnd, inorder, inorderLeftBegin, inorderLeftEnd);
        root.right = dfs(preorder, preorderRightBegin, preorderRightEnd, inorder, inorderRightBegin, inorderRightEnd);
        return root;
    }

前序和后序遍历无法构造一颗二叉树,因为没有中序来区分左右子树。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值