LeetCode 题解工作台

使二叉树所有路径值相等的最小代价

给你一个整数 n 表示一棵 满二叉树 里面节点的数目,节点编号从 1 到 n 。根节点编号为 1 ,树中每个非叶子节点 i 都有两个孩子,分别是左孩子 2 * i 和右孩子 2 * i + 1 。 树中每个节点都有一个值,用下标从 0 开始、长度为 n 的整数数组 cost 表示,其中 cost[i…

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·树·traversal

bolt

答案摘要

根据题目描述,我们需要计算最小的增加次数,使得根节点到每个叶子节点的路径值相等。 根节点到每个叶子节点的路径值相等,实际上等价于以任意节点为根节点的子树到该子树的每个叶子节点的路径值相等。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数 n 表示一棵 满二叉树 里面节点的数目,节点编号从 1 到 n 。根节点编号为 1 ,树中每个非叶子节点 i 都有两个孩子,分别是左孩子 2 * i 和右孩子 2 * i + 1 。

树中每个节点都有一个值,用下标从 0 开始、长度为 n 的整数数组 cost 表示,其中 cost[i] 是第 i + 1 个节点的值。每次操作,你可以将树中 任意 节点的值 增加 1 。你可以执行操作 任意 次。

你的目标是让根到每一个 叶子结点 的路径值相等。请你返回 最少 需要执行增加操作多少次。

注意:

  • 满二叉树 指的是一棵树,它满足树中除了叶子节点外每个节点都恰好有 2 个子节点,且所有叶子节点距离根节点距离相同。
  • 路径值 指的是路径上所有节点的值之和。

 

示例 1:

输入:n = 7, cost = [1,5,2,2,3,3,1]
输出:6
解释:我们执行以下的增加操作:
- 将节点 4 的值增加一次。
- 将节点 3 的值增加三次。
- 将节点 7 的值增加两次。
从根到叶子的每一条路径值都为 9 。
总共增加次数为 1 + 3 + 2 = 6 。
这是最小的答案。

示例 2:

输入:n = 3, cost = [5,3,3]
输出:0
解释:两条路径已经有相等的路径值,所以不需要执行任何增加操作。

 

提示:

  • 3 <= n <= 105
  • n + 1 是 2 的幂
  • cost.length == n
  • 1 <= cost[i] <= 104
lightbulb

解题思路

方法一:贪心

根据题目描述,我们需要计算最小的增加次数,使得根节点到每个叶子节点的路径值相等。

根节点到每个叶子节点的路径值相等,实际上等价于以任意节点为根节点的子树到该子树的每个叶子节点的路径值相等。

为什么呢?我们可以用反证法来证明。假设存在一个节点 xx,以它为根节点的子树到某个叶子节点的路径值不相等,那么也就存在着根节点到叶子节点的路径值不相等的情况,这与“根节点到每个叶子节点的路径值相等”矛盾。因此假设不成立,以任意节点为根节点的子树到该子树的每个叶子节点的路径值相等。

我们可以从树的底部开始,逐层向上计算增加次数。对于每个非叶子节点,我们可以计算它的左右孩子节点的路径值,增加的次数为两者路径值的差值,然后将左右孩子节点的路径值更新为两者中的较大值。

最后返回总的增加的次数即可。

时间复杂度 O(n)O(n),其中 nn 为节点数。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
class Solution:
    def minIncrements(self, n: int, cost: List[int]) -> int:
        ans = 0
        for i in range(n >> 1, 0, -1):
            l, r = i << 1, i << 1 | 1
            ans += abs(cost[l - 1] - cost[r - 1])
            cost[i - 1] += max(cost[l - 1], cost[r - 1])
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Look for a clear understanding of binary tree traversal techniques (DFS/BFS).

  • question_mark

    Evaluate the candidate's ability to optimize the solution using dynamic programming or greedy strategies.

  • question_mark

    Assess how well the candidate balances time complexity and correctness, particularly for large inputs.

warning

常见陷阱

外企场景
  • error

    Forgetting to adjust node costs optimally, leading to unnecessary increments.

  • error

    Not recognizing the importance of path cost comparison between the highest and others.

  • error

    Overcomplicating the solution without leveraging greedy or dynamic programming techniques effectively.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Adjusting the tree structure by introducing non-perfect binary trees.

  • arrow_right_alt

    Incorporating constraints where cost increases are limited to a fixed range.

  • arrow_right_alt

    Extending the problem to multiple trees with varying costs and structure.

help

常见问题

外企场景

使二叉树所有路径值相等的最小代价题解:二分·树·traversal | LeetCode #2673 中等