二叉树的坡度
难度:
标签:
题目描述
Given the root
of a binary tree, return the sum of every tree node's tilt.
The tilt of a tree node is the absolute difference between the sum of all left subtree node values and all right subtree node values. If a node does not have a left child, then the sum of the left subtree node values is treated as 0
. The rule is similar if the node does not have a right child.
Example 1:

Input: root = [1,2,3] Output: 1 Explanation: Tilt of node 2 : |0-0| = 0 (no children) Tilt of node 3 : |0-0| = 0 (no children) Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3) Sum of every tilt : 0 + 0 + 1 = 1
Example 2:

Input: root = [4,2,9,3,5,null,7] Output: 15 Explanation: Tilt of node 3 : |0-0| = 0 (no children) Tilt of node 5 : |0-0| = 0 (no children) Tilt of node 7 : |0-0| = 0 (no children) Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5) Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7) Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16) Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
Example 3:

Input: root = [21,7,14,1,1,2,2,3,3] Output: 9
Constraints:
- The number of nodes in the tree is in the range
[0, 104]
. -1000 <= Node.val <= 1000
代码结果
运行时间: 18 ms, 内存: 17.2 MB
/*
* 思路:
* 1. 使用递归来计算每个节点的坡度,同时用流的方式来累加总坡度。
* 2. 使用一个数组来保存总坡度,递归遍历每个节点并计算左右子树的和。
* 3. 通过流式处理来计算和累加坡度。
*/
import java.util.stream.Stream;
public class Solution {
public int findTilt(TreeNode root) {
return Stream.of(new int[1]).mapToInt(result -> calculateSum(root, result)).sum(); // 计算总坡度
}
private int calculateSum(TreeNode node, int[] result) {
if (node == null) {
return 0; // 空节点返回0
}
int leftSum = calculateSum(node.left, result); // 左子树的和
int rightSum = calculateSum(node.right, result); // 右子树的和
result[0] += Math.abs(leftSum - rightSum); // 当前节点的坡度累加到总坡度
return leftSum + rightSum + node.val; // 返回当前子树的总和
}
}
解释
方法:
题解采用递归方法遍历二叉树的每个节点,并计算每个节点的坡度。函数sumTree是一个递归函数,它计算并返回当前节点及其所有子节点的值之和。在计算过程中,该函数还计算当前节点左右子树的值之和的差的绝对值,并累加到全局变量self.ans中,以计算整棵树的坡度总和。递归的基础情况是当遇到空节点时,返回0,表示该节点的值之和为0。
时间复杂度:
O(n)
空间复杂度:
O(h),其中h是树的高度
代码细节讲解
🦆
在递归函数sumTree中,你是如何确保每个节点的坡度只被计算一次的?
▷🦆
递归函数sumTree返回的是当前节点及其子节点的总和,那么这个返回值是如何在计算坡度时被利用的?
▷🦆
你是如何处理空节点的情况,以防在计算坡度时出现null引用错误?
▷