LeetCode 105. 从前序与中序遍历序列构造二叉树, 106. 从中序与后序遍历序列构造二叉树

  1. 如何记住前中后序的遍历顺序:
    前序:中左右, 中序:左中右, 后续:左右中。
    观察发现,这个前中后序就是"中"所在的位置。
  2. 首先明确一点:前序和中序以及后序和中序都可以确定一棵二叉树,然而前序和后序却不能,为什么呢?因为我们只有知道中序才可以知道左子树,右子树的位置,才可以区分开来。从而还原一棵二叉树。
  3. 那么105,106两题就可以做了,题中有说到,节点的值都是不相等的,所以可以确定唯一一颗二叉树。

具体解题思路可以观看代码随想录题解,本文不再进行搬运,而是分析一些问题。

大概分为以下几步:

第一步:如果数组大小为零的话,说明是空节点了。

第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。

第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点

第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)

第五步:切割后序数组,切成后序左数组和后序右数组

第六步:递归处理左区间和右区间

  1. Java对于数组的操作不是很灵活,需要转成ArrayList<>的形式,才会有indexOf之类的操作。
    所以我们用HashMap来进行index。
  2. 边界问题:
    1. 如果传入的是size-1,结束判断没有等号。所谓的左开右闭应当如此。
    2. 递归过程中,End也应该是End,不应该-1。
    3. index应该取index-1,index+1不应包含index。

105. 从前序与中序遍历序列构造二叉树

java 复制代码
class Solution {
    HashMap<Integer, Integer> inordermap = new HashMap<>();
    int[] pre;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        for (int i = 0; i < inorder.length; i++) {
            inordermap.put(inorder[i], i);
        }
        pre = preorder;
        TreeNode root = dfs(0, preorder.length - 1, 0, inorder.length - 1);
        return root;
    }

    private TreeNode dfs(int preStart, int preEnd, int inStart, int inEnd) {
        if (preEnd < preStart || inEnd < inStart) return null;
        int root = pre[preStart];
        int index = inordermap.get(root);
        
        TreeNode node = new TreeNode(root);
        node.left = dfs(preStart + 1, preStart + index - inStart, inStart, index - 1);
        node.right = dfs(preStart + index - inStart + 1, preEnd, index + 1, inEnd);
        return node;
    }
}

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

java 复制代码
class Solution {
    HashMap<Integer, Integer> inordermap = new HashMap<>();
    int[] post;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for (int i = 0; i < inorder.length; i++) {
            inordermap.put(inorder[i], i);
        }
        post = postorder;
        TreeNode root = dfs(0, inorder.length - 1, 0, post.length - 1);
        return root;
    }

    public TreeNode dfs(int inorderStart, int inorderEnd, int postorderStart, int postorderEnd) {
        if (inorderEnd < inorderStart || postorderEnd < postorderStart) {
            return null;
        }
        int root = post[postorderEnd];
        int index = inordermap.get(root);

        TreeNode node = new TreeNode(root);
        node.left = dfs(inorderStart, index - 1, postorderStart, postorderStart + index - inorderStart - 1);
        node.right = dfs(index + 1, inorderEnd, postorderStart + index - inorderStart, postorderEnd - 1);
        return node;
    }
}
相关推荐
NAGNIP5 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
颜酱13 小时前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub16 小时前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub17 小时前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub17 小时前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub17 小时前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub17 小时前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
NAGNIP1 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP1 天前
一文搞懂激活函数!
算法·面试
董董灿是个攻城狮1 天前
AI 视觉连载7:传统 CV 之高斯滤波实战
算法