【二叉树】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),存储索引的哈希表和递归调用栈的空间。
相关推荐
CoovallyAIHub19 分钟前
推理提速一倍!SegDT:轻量化扩散 Transformer,医学图像分割的技术跨越
深度学习·算法·计算机视觉
CoovallyAIHub28 分钟前
无人机方案如何让桥梁监测更安全、更智能?融合RTK与超高分辨率成像,优于毫米精度
深度学习·算法·计算机视觉
lingran__31 分钟前
C语言制作扫雷游戏(拓展版赋源码)
c语言·算法·游戏
序属秋秋秋32 分钟前
《C++进阶之STL》【set/map 使用介绍】
开发语言·c++·笔记·leetcode·stl·set·map
self_myth33 分钟前
【考研/面试必备】操作系统核心原理与IPO机制深度解析
大数据·算法
Greedy Alg1 小时前
LeetCode 240. 搜索二维矩阵 II
算法·leetcode·职场和发展
墨染点香1 小时前
LeetCode 刷题【68. 文本左右对齐】
算法·leetcode·职场和发展
GalaxyPokemon1 小时前
LeetCode - 202. 快乐数
算法·leetcode·职场和发展
吃着火锅x唱着歌1 小时前
LeetCode 522.最长特殊序列2
算法·leetcode·职场和发展
初级炼丹师(爱说实话版)1 小时前
2025算法八股——机器学习——SVM损失函数
算法·机器学习·支持向量机