【二叉树】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),存储索引的哈希表和递归调用栈的空间。
相关推荐
小鱼在乎几秒前
动态规划---最长回文子序列
算法·动态规划
xiaobai12 35 分钟前
二叉树的遍历【C++】
开发语言·c++·算法
吱吱鼠叔30 分钟前
MATLAB数学规划:2.线性规划
算法·机器学习·matlab
声学黑洞仿真工作室37 分钟前
Matlab Delany-Bazley和Miki模型预测多孔材料吸声性能
开发语言·人工智能·算法·matlab·微信公众平台
机器学习之心42 分钟前
选址模型 | 基于混沌模拟退火粒子群优化算法的电动汽车充电站选址与定容(Matlab)
算法·选址模型
MogulNemenis1 小时前
力扣春招100题——队列
数据结构·算法·leetcode
码上一元1 小时前
【百日算法计划】:每日一题,见证成长(017)
java·算法
菜鸟求带飞_2 小时前
算法打卡:第十一章 图论part01
java·数据结构·算法
浅念同学2 小时前
算法.图论-建图/拓扑排序及其拓展
算法·图论
是小Y啦2 小时前
leetcode 106.从中序与后续遍历序列构造二叉树
数据结构·算法·leetcode