【二叉树】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),存储索引的哈希表和递归调用栈的空间。
相关推荐
机器学习之心6 分钟前
机器学习用于算法交易(Matlab实现)
算法·机器学习·matlab
AL流云。6 分钟前
【优选算法】C++滑动窗口
数据结构·c++·算法
qq_429879671 小时前
省略号和可变参数模板
开发语言·c++·算法
飞川撸码2 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
Neil今天也要学习2 小时前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法
yzx9910133 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
亮亮爱刷题3 小时前
算法练习-回溯
算法
眼镜哥(with glasses)4 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
int型码农9 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT9 小时前
NoSQL之redis哨兵
java·前端·算法