LeetCode重建二叉树系列问题总结

二叉树天然的递归特性,使得我们可以使用递归算法对二叉树进行遍历和重建。之前已经写过LeetCode二叉树的前序、中序、后序遍历(递归实现),那么本文将进行二叉树的重建,经过对比,会发现二者有着许多相似之处。

准备工作

二叉树节点定义:

//Definition for a binary tree node.
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

需要用到数组部分复制的API:


LeetCode题解

LeetCode上面关于二叉树重建的问题有:        

#
Title
105
106
889
 

 105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree.

class Solution {
    public TreeNode buildTree(int[] pre, int[] in) {
        if (pre.length == 0) {
            return null;
        }
        System.out.println(pre[0]);
        TreeNode res = new TreeNode(pre[0]);
        int index = getIndex(in, pre[0]);
        res.left = buildTree(Arrays.copyOfRange(pre, 1, index +  1), Arrays.copyOfRange(in, 0, index));
        res.right = buildTree(Arrays.copyOfRange(pre, index + 1,  pre.length),
                Arrays.copyOfRange(in, index + 1, in.length));
        return res;
    }
    public static int getIndex(int[] arr, int value) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == value) {
                return i;
            }
        }
        return -1;
    }
}

106. Construct Binary Tree from Inorder and Postorder Traversal

 Given inorder and postorder traversal of a tree, construct the binary tree.

class Solution {
    public TreeNode buildTree(int[] in, int[] post) {
        if (in.length == 0 || post.length == 0) {
            return null;
        }
        if (in.length == 1) {
            return new TreeNode(in[0]);
        }
        TreeNode res = new TreeNode(post[post.length - 1]);
        int index = getIndex(in, post[post.length - 1]);
        res.left = buildTree(Arrays.copyOfRange(in, 0, index),  Arrays.copyOfRange(post, 0, index));
        res.right = buildTree(Arrays.copyOfRange(in, index + 1,  in.length),
                Arrays.copyOfRange(post, index, post.length -  1));
        return res;
    }
    public static int getIndex(int[] arr, int value) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == value) {
                return i;
            }
        }
        return -1;
    }
}

889. Construct Binary Tree from Preorder and Postorder Traversal

Return any binary tree that matches the given preorder and postorder traversals.

Values in the traversals pre and post are distinct positive integers.

class Solution {
    public TreeNode constructFromPrePost(int[] pre, int[] post)  {
        if (pre.length == 0) {
            return null;
        }
        if (pre.length == 1) {
            return new TreeNode(pre[0]);
        }
        TreeNode res = new TreeNode(pre[0]);
        int index = getIndex(pre, post[post.length - 2]);
        res.left = constructFromPrePost(Arrays.copyOfRange(pre,  1, index), Arrays.copyOfRange(post, 0, index - 1));
        res.right = constructFromPrePost(Arrays.copyOfRange(pre,  index, pre.length),
                Arrays.copyOfRange(post, index - 1, post.length -  1));
        return res;
    }
    public static int getIndex(int[] arr, int value) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == value) {
                return i;
            }
        }
        return -1;
    }
}

 

转载于:https://www.cnblogs.com/sgh1023/p/10442542.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值