【LeetCodehot100】T114:二叉树展开为链表 T105:从前序与中序遍历构造二叉树

T114:二叉树展开为链表

题目要求:

给你二叉树的根结点 root ,请你将它展开为一个单链表:

展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。

展开后的单链表应该与二叉树 先序遍历 顺序相同。

核心思路

前序遍历顺序+指针重排

具体操作:

  1. 先展开左子树
  2. 再展开右子树
  3. 把左子树移到右边
  4. 把原右子树接到末尾

代码实现

java 复制代码
public void flatten(TreeNode root) {
    if (root == null) return;

    // 1️⃣ 递归展开左右子树
    flatten(root.left);
    flatten(root.right);

    // 2️⃣ 保存原右子树
    TreeNode right = root.right;

    // 3️⃣ 左子树移到右边
    root.right = root.left;
    root.left = null;

    // 4️⃣ 找右链尾节点
    TreeNode cur = root;
    while (cur.right != null) {
        cur = cur.right;
    }

    // 5️⃣ 接上原右子树
    cur.right = right;
}

本题感悟

掌握前序遍历顺序+指针重排的具体流程

  1. 先展开左子树
  2. 再展开右子树
  3. 把左子树移到右边
  4. 把原右子树接到末尾

T105:从前序与中序遍历构造二叉树

题目要求:

给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

核心思路

前序遍历

根 → 左 → 右

作用:

确定"根节点"
中序遍历

左 → 根 → 右

作用:

划分左右子树
前序找根,中序分左右,递归构建

思路

1️⃣ 用 preorder[preL] 找到根

2️⃣ 在 inorder 中找到根的位置 index

3️⃣ 计算左子树大小 leftSize

4️⃣ 划分左右子树范围

5️⃣ 递归构建左右子树

代码实现

java 复制代码
class Solution {

    Map<Integer, Integer> map = new HashMap<>();

    public TreeNode buildTree(int[] preorder, int[] inorder) {

        // 建立中序索引
        for (int i = 0; i < inorder.length; i++) {
            map.put(inorder[i], i);
        }

        return build(preorder, 0, preorder.length - 1,
                     inorder, 0, inorder.length - 1);
    }

    private TreeNode build(int[] preorder, int preL, int preR,
                           int[] inorder, int inL, int inR) {

        if (preL > preR) return null;

        // 1️⃣ 根节点
        int rootVal = preorder[preL];
        TreeNode root = new TreeNode(rootVal);

        // 2️⃣ 找中序位置
        int index = map.get(rootVal);

        // 3️⃣ 左子树大小
        int leftSize = index - inL;

        // 4️⃣ 左子树
        root.left = build(preorder, preL + 1, preL + leftSize,
                          inorder, inL, index - 1);

        // 5️⃣ 右子树
        root.right = build(preorder, preL + leftSize + 1, preR,
                           inorder, index + 1, inR);

        return root;
    }
}
相关推荐
灰色小旋风1 小时前
力扣20有效的括号(C++)
c++·算法·leetcode·职场和发展
SuniaWang1 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题八:《RAG 系统安全与权限管理:企业级数据保护方案》
java·前端·人工智能·spring boot·后端·spring·架构
逆境不可逃2 小时前
LeetCode 热题 100 之 160. 相交链表 206. 反转链表 234. 回文链表 141. 环形链表 142. 环形链表 II
算法·leetcode·链表
CoovallyAIHub2 小时前
AAAI 2026 | 华中科大联合清华等提出Anomagic:跨模态提示零样本异常生成+万级AnomVerse数据集(附代码)
深度学习·算法·计算机视觉
npupengsir2 小时前
nano vllm代码详解
人工智能·算法·vllm
xiaohe072 小时前
Maven Spring框架依赖包
java·spring·maven
m0_569881472 小时前
C++中的组合模式高级应用
开发语言·c++·算法
m0_730115112 小时前
高性能计算负载均衡
开发语言·c++·算法
灰色小旋风2 小时前
力扣19删除链表的倒数第N个结点(C++)
c++·算法·leetcode·链表