【二叉树】Leetcode 106. 从中序与后序遍历序列构造二叉树【中等】

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

  • 给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

示例 1:


输入 :inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]

解题思路

后序遍历: 左子树 -> 右子树 -> 根节点,最后一个元素是树的根节点。

中序遍历:左子树 ->根节点-> 右子树 ,根节点的左边是左子树的节点,右边是右子树的节点。

递归构建树:

  • 从后序遍历的最后一个元素开始,确定根节点。
  • 在中序遍历中找到根节点的位置,确定左子树和右子树的范围。
  • 递归构建左子树和右子树。

java实现

java 复制代码
public class ConstructBinaryTreeByInorderAndPostorder {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    private Map<Integer, Integer> inorderIndexMap;
    private int postIndex;

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        inorderIndexMap = new HashMap<>();
        postIndex = postorder.length - 1;

        // 构建中序遍历值到索引的映射
        for (int i = 0; i < inorder.length; i++) {
            inorderIndexMap.put(inorder[i], i);
        }

        return buildTreeHelper(inorder, postorder, 0, inorder.length - 1);
    }

    private TreeNode buildTreeHelper(int[] inorder, int[] postorder, int inorderStart, int inorderEnd) {
        if (inorderStart > inorderEnd) {
            return null;
        }

        // 获取当前子树的根节点
        int rootVal = postorder[postIndex--];
        TreeNode root = new TreeNode(rootVal);

        // 获取根节点在中序遍历中的索引
        int inorderIndex = inorderIndexMap.get(rootVal);

        // 构建右子树和左子树(注意顺序,先右后左,
        // 因为后续遍历顺序是左子树 -> 右子树 -> 根节点
        // 处理完根就要先处理理右子树节点)
        root.right = buildTreeHelper(inorder, postorder, inorderIndex + 1, inorderEnd);
        root.left = buildTreeHelper(inorder, postorder, inorderStart, inorderIndex - 1);

        return root;
    }

    // 辅助方法:打印二叉树(中序遍历)
    public static void printInOrder(TreeNode root) {
        if (root != null) {
            printInOrder(root.left);
            System.out.print(root.val + " ");
            printInOrder(root.right);
        }
    }

    // 辅助方法:打印二叉树(后序遍历)
    public static void printPostOrder(TreeNode root) {
        if (root != null) {
            printPostOrder(root.left);
            printPostOrder(root.right);
            System.out.print(root.val + " ");
        }
    }

    public static void main(String[] args) {
        ConstructBinaryTreeByInorderAndPostorder constructBinaryTreeByInorderAndPostorder = new ConstructBinaryTreeByInorderAndPostorder();

        // 示例输入
        int[] inorder = {9, 3, 15, 20, 7};
        int[] postorder = {9, 15, 7, 20, 3};

        // 构建二叉树
        TreeNode root = constructBinaryTreeByInorderAndPostorder.buildTree(inorder, postorder);

        // 打印二叉树
        System.out.print("InOrder: ");
        printInOrder(root); // 输出: 9 3 15 20 7
        System.out.println();
        System.out.print("PostOrder: ");
        printPostOrder(root); // 输出: 9 15 7 20 3
    }
}

时间空间复杂度

  • 时间复杂度:O(n),其中 n 是树节点的数量。需要遍历中序和后序数组来构建树。
  • 空间复杂度:O(n),存储索引的哈希表和递归调用栈的空间。
相关推荐
CodeWizard~5 分钟前
AtCoder Beginner Contest 430赛后补题
c++·算法·图论
大大dxy大大16 分钟前
机器学习-KNN算法示例
人工智能·算法·机器学习
关于不上作者榜就原神启动那件事1 小时前
模拟算法乒乓球
开发语言·c++·算法
Bug退退退1231 小时前
ArrayList 与 LinkedList 的区别
java·数据结构·算法
88号技师2 小时前
2025年7月一区SCI优化算法-Logistic-Gauss Circle optimizer-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法
一个不知名程序员www3 小时前
算法学习入门---二分查找(C++)
c++·算法
2301_807997383 小时前
代码随想录-day26
数据结构·c++·算法·leetcode
闭着眼睛学算法3 小时前
【双机位A卷】华为OD笔试之【排序】双机位A-银行插队【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
TL滕3 小时前
从0开始学算法——第一天(认识算法)
数据结构·笔记·学习·算法
小欣加油3 小时前
leetcode 3318 计算子数组的x-sum I
c++·算法·leetcode·职场和发展