LeetCode 题解工作台

递增顺序搜索树

给你一棵二叉搜索树的 root ,请你 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没有左子节点,只有一个右子节点。 示例 1: 输入: root = [5,3,6,2,4,null,8,1,null,null,null,7,9] 输出: [1,nul…

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 二分·树·traversal

bolt

答案摘要

我们定义一个虚拟节点 ,初始时 的右子节点指向根节点 ,定义一个指针 指向 。 我们对二叉搜索树进行中序遍历,遍历过程中,每遍历到一个节点,就将 的右子节点指向它,然后将当前节点的左子节点置为空,再将当前节点赋值给 ,以便于下一次遍历。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一棵二叉搜索树的 root ,请你 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没有左子节点,只有一个右子节点。

 

示例 1:

输入:root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
输出:[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]

示例 2:

输入:root = [5,1,7]
输出:[1,null,5,null,7]

 

提示:

  • 树中节点数的取值范围是 [1, 100]
  • 0 <= Node.val <= 1000
lightbulb

解题思路

方法一:DFS 中序遍历

我们定义一个虚拟节点 dummydummy,初始时 dummydummy 的右子节点指向根节点 rootroot,定义一个指针 prevprev 指向 dummydummy

我们对二叉搜索树进行中序遍历,遍历过程中,每遍历到一个节点,就将 prevprev 的右子节点指向它,然后将当前节点的左子节点置为空,再将当前节点赋值给 prevprev,以便于下一次遍历。

遍历结束后,原二叉搜索树被修改成只有右子节点的单链表,我们再将虚拟节点 dummydummy 的右子节点返回即可。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 为二叉搜索树的节点个数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def increasingBST(self, root: TreeNode) -> TreeNode:
        def dfs(root):
            if root is None:
                return
            nonlocal prev
            dfs(root.left)
            prev.right = root
            root.left = None
            prev = root
            dfs(root.right)

        dummy = prev = TreeNode(right=root)
        dfs(root)
        return dummy.right
speed

复杂度分析

指标
时间complexity is O(n) because every node is visited exactly once during traversal. Space complexity is O(h) for recursion stack or O(n) for iterative stack in the worst case, where h is the tree height and n is the number of nodes.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Checking for correct in-order traversal and reconnection of nodes

  • question_mark

    Looking for proper nullification of left children to maintain right-skewed structure

  • question_mark

    Expecting handling of both recursive and iterative DFS approaches for clarity and efficiency

warning

常见陷阱

外企场景
  • error

    Forgetting to set left children to null, causing cycles or incorrect tree shape

  • error

    Not maintaining a previous node reference, which breaks the increasing sequence linkage

  • error

    Assuming a new array output rather than reconstructing the tree nodes, which is inefficient

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Create a decreasing order search tree by reversing in-order traversal

  • arrow_right_alt

    Flatten the BST to a linked list in-place with both left and right pointers set to null or used for next node

  • arrow_right_alt

    Apply the same in-order reconstruction approach to a general binary tree, not strictly a BST

help

常见问题

外企场景

递增顺序搜索树题解:二分·树·traversal | LeetCode #897 简单