LeetCode 题解工作台

二叉树的坡度

给你一个二叉树的根节点 root ,计算并返回 整个树 的坡度 。 一个树的 节点的坡度 定义即为,该节点左子树的节点之和和右子树节点之和的 差的绝对值 。如果没有左子树的话,左子树的节点之和为 0 ;没有右子树的话也是一样。空结点的坡度是 0 。 整个树 的坡度就是其所有节点的坡度之和。 示例 1…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 二分·树·traversal

bolt

答案摘要

我们设计一个函数 ,用来计算以当前节点为根节点的子树的节点之和。在 函数中,我们首先判断当前节点是否为空,若为空则返回 0。然后递归调用 函数计算左子树的节点之和 和右子树的节点之和 。接着计算当前节点的坡度,即 $|l - r|$,并将其加到答案中。最后返回当前节点的节点之和 $l + r + \textit{root.val}$。 在主函数中,我们初始化答案为 0,然后调用 函数计算整…

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个二叉树的根节点 root ,计算并返回 整个树 的坡度 。

一个树的 节点的坡度 定义即为,该节点左子树的节点之和和右子树节点之和的 差的绝对值 。如果没有左子树的话,左子树的节点之和为 0 ;没有右子树的话也是一样。空结点的坡度是 0 。

整个树 的坡度就是其所有节点的坡度之和。

 

示例 1:

输入:root = [1,2,3]
输出:1
解释:
节点 2 的坡度:|0-0| = 0(没有子节点)
节点 3 的坡度:|0-0| = 0(没有子节点)
节点 1 的坡度:|2-3| = 1(左子树就是左子节点,所以和是 2 ;右子树就是右子节点,所以和是 3 )
坡度总和:0 + 0 + 1 = 1

示例 2:

输入:root = [4,2,9,3,5,null,7]
输出:15
解释:
节点 3 的坡度:|0-0| = 0(没有子节点)
节点 5 的坡度:|0-0| = 0(没有子节点)
节点 7 的坡度:|0-0| = 0(没有子节点)
节点 2 的坡度:|3-5| = 2(左子树就是左子节点,所以和是 3 ;右子树就是右子节点,所以和是 5 )
节点 9 的坡度:|0-7| = 7(没有左子树,所以和是 0 ;右子树正好是右子节点,所以和是 7 )
节点 4 的坡度:|(3+5+2)-(9+7)| = |10-16| = 6(左子树值为 3、5 和 2 ,和是 10 ;右子树值为 9 和 7 ,和是 16 )
坡度总和:0 + 0 + 0 + 2 + 7 + 6 = 15

示例 3:

输入:root = [21,7,14,1,1,2,2,3,3]
输出:9

 

提示:

  • 树中节点数目的范围在 [0, 104]
  • -1000 <= Node.val <= 1000
lightbulb

解题思路

方法一:递归

我们设计一个函数 dfs\text{dfs},用来计算以当前节点为根节点的子树的节点之和。在 dfs\text{dfs} 函数中,我们首先判断当前节点是否为空,若为空则返回 0。然后递归调用 dfs\text{dfs} 函数计算左子树的节点之和 ll 和右子树的节点之和 rr。接着计算当前节点的坡度,即 lr|l - r|,并将其加到答案中。最后返回当前节点的节点之和 l+r+root.vall + r + \textit{root.val}

在主函数中,我们初始化答案为 0,然后调用 dfs\text{dfs} 函数计算整个树的坡度,并返回答案。

时间复杂度 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
# 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 findTilt(self, root: Optional[TreeNode]) -> int:
        def dfs(root: Optional[TreeNode]) -> int:
            if root is None:
                return 0
            l, r = dfs(root.left), dfs(root.right)
            nonlocal ans
            ans += abs(l - r)
            return l + r + root.val

        ans = 0
        dfs(root)
        return ans
speed

复杂度分析

指标
时间\mathcal{O}(N)
空间\mathcal{O}(N)
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidate demonstrates clear understanding of tree traversal techniques.

  • question_mark

    Candidate can efficiently manage state while performing the DFS.

  • question_mark

    Candidate is able to articulate the trade-offs between different traversal methods.

warning

常见陷阱

外企场景
  • error

    Forgetting to track the sum of each subtree, leading to incorrect tilt calculations.

  • error

    Not properly handling cases where a node has no children, causing incorrect tilt values.

  • error

    Misunderstanding the problem and trying to calculate the tilt with a more complex traversal.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Alter the problem to return the tilt of a subtree rather than the entire tree's tilt.

  • arrow_right_alt

    Extend the problem to handle nodes with more than two children.

  • arrow_right_alt

    Modify the input to be a multi-level list instead of a binary tree representation.

help

常见问题

外企场景

二叉树的坡度题解:二分·树·traversal | LeetCode #563 简单