【二叉树】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),存储索引的哈希表和递归调用栈的空间。
相关推荐
Moshow郑锴23 分钟前
机器学习相关算法:回溯算法 贪心算法 回归算法(线性回归) 算法超参数 多项式时间 朴素贝叶斯分类算法
算法·机器学习·回归
liulilittle41 分钟前
BFS寻路算法解析与实现
开发语言·c++·算法·宽度优先·寻路算法·寻路
剪一朵云爱着1 小时前
PAT 1065 A+B and C (64bit)
算法·pat考试
喜欢吃燃面1 小时前
C++算法竞赛:位运算
开发语言·c++·学习·算法
项目申报小狂人2 小时前
算法应用上新!自适应更新策略差分进化算法求解球形多飞行器路径规划问题,附完整MATLAB代码
开发语言·算法·matlab
PAK向日葵8 小时前
【算法导论】PDD 0817笔试题题解
算法·面试
地平线开发者10 小时前
ReID/OSNet 算法模型量化转换实践
算法·自动驾驶
地平线开发者10 小时前
开发者说|EmbodiedGen:为具身智能打造可交互3D世界生成引擎
算法·自动驾驶
星星火柴93611 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法