Binary Tree Tilt Problem
Description
LeetCode Problem 563.
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 there 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, 10^4].
- -1000 <= Node.val <= 1000
Solution
Approach: Post-Order DFS
A node's tilt needs the sums of its left and right subtrees. Computing those sums from scratch at every node would revisit the same nodes again and again, O(n2) in total for a chain-like tree. Instead, let one recursive function return a subtree's sum, and compute the tilt from the children's results while we are there.
subtreeSum(node):
- If
nodeis null, return 0. - Get
left = subtreeSum(node.left)andright = subtreeSum(node.right). - The tilt of
nodeis|left - right|; add it to a runningtotalTilt. - Return
left + right + node.val, the sum of the whole subtree, for the parent to use.
Call it on the root and return totalTilt. This is a post-order traversal: both children are finished before their parent, so each subtree sum is ready exactly when it is needed.
The mix-up to avoid is returning the tilt instead of the sum. The parent needs the sum of the subtree's values; the tilts are collected separately in totalTilt. In Example 2, node 2 has tilt 2 but passes up the sum 3 + 5 + 2 = 10, which gives the root its tilt |10 - 16| = 6.
Complexity: O(n) time, where n is the number of nodes, since each node is visited once. O(h) space for the recursion stack, where h is the height of the tree.
class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
total_tilt = 0
def subtree_sum(node: Optional[TreeNode]) -> int:
"""Sum of all values in node's subtree; adds node's tilt to total_tilt on the way."""
nonlocal total_tilt
if node is None:
return 0
left = subtree_sum(node.left)
right = subtree_sum(node.right)
total_tilt += abs(left - right)
return left + right + node.val
subtree_sum(root)
return total_tilt