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上面关于二叉树重建的问题有:        

 

 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;
    }
}
复制代码

 

posted @   James_Shangguan  阅读(437)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示