LeetCode 题解工作台

把二叉搜索树转换为累加树

给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。 提醒一下,二叉搜索树满足下列约束条件: 节点的左子树仅包含键 小于 节点键的节点。 节点的右子树仅包含键 大于 …

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·树·traversal

bolt

答案摘要

按照“右根左”的顺序,递归遍历二叉搜索树,累加遍历到的所有节点值到 中,然后每次赋值给对应的 `node` 节点。 时间复杂度 ,空间复杂度 。其中 是二叉搜索树的节点数。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。

提醒一下,二叉搜索树满足下列约束条件:

  • 节点的左子树仅包含键 小于 节点键的节点。
  • 节点的右子树仅包含键 大于 节点键的节点。
  • 左右子树也必须是二叉搜索树。

注意:本题和 1038: https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/ 相同

 

示例 1:

输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

示例 2:

输入:root = [0,null,1]
输出:[1,null,1]

示例 3:

输入:root = [1,0,2]
输出:[3,3,2]

示例 4:

输入:root = [3,2,4,1]
输出:[7,9,4,10]

 

提示:

  • 树中的节点数介于 0 和 104 之间。
  • 每个节点的值介于 -104 和 104 之间。
  • 树中的所有值 互不相同
  • 给定的树为二叉搜索树。
lightbulb

解题思路

方法一:递归

按照“右根左”的顺序,递归遍历二叉搜索树,累加遍历到的所有节点值到 ss 中,然后每次赋值给对应的 node 节点。

时间复杂度 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
# 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 convertBST(self, root: TreeNode) -> TreeNode:
        def dfs(root):
            nonlocal s
            if root is None:
                return
            dfs(root.right)
            s += root.val
            root.val = s
            dfs(root.left)

        s = 0
        dfs(root)
        return root
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate handles tree traversal correctly and efficiently.

  • question_mark

    Assess how well the candidate manages state tracking during DFS traversal.

  • question_mark

    Evaluate whether the candidate ensures the space complexity remains minimal by avoiding extra data structures.

warning

常见陷阱

外企场景
  • error

    Failing to handle the reverse in-order traversal properly, which leads to incorrect accumulation of greater values.

  • error

    Not updating the current node’s value before moving to its left child.

  • error

    Using unnecessary extra space or auxiliary data structures that increase space complexity unnecessarily.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Implementing an iterative solution instead of recursion using a stack to simulate the DFS traversal.

  • arrow_right_alt

    Optimizing the solution to handle very large trees with limited memory by using an iterative approach.

  • arrow_right_alt

    Modifying the tree without recursion or stack, possibly by reordering the nodes in a non-recursive manner.

help

常见问题

外企场景

把二叉搜索树转换为累加树题解:二分·树·traversal | LeetCode #538 中等