Binary Tree Tilt (LeetCode 563): Solution and Explanation

Easy
Tree
Depth First Search
Binary Tree

The tilt of a node is the absolute difference between the sums of its left and right subtrees. Return the sum of the tilts of all nodes.

Intuition

To find one node's tilt you need two numbers: the total of everything in its left subtree and the total of everything in its right subtree.

Computing those sums separately for every node repeats work, because a parent's subtree sum contains its children's sums. A child's sum plus its sibling's sum plus the parent's value is the parent's sum.

So let a post-order function return the subtree sum. At the moment it has both child sums, it also has everything needed for the tilt, so it adds |left - right| to a running total before returning.

Approach: Post-order subtree sums

  1. Keep a running total, starting at 0.
  2. subtree_sum(node) returns 0 for a null node.
  3. Otherwise compute left = subtree_sum(node.left) and right = subtree_sum(node.right).
  4. Add abs(left - right) to total.
  5. Return left + right + node.val.
  6. Call it on the root and return total.

Step-by-step example

root = [1,2,3]
  1. Call subtree_sum(1). It first recurses left.
  2. Node 2 is a leaf: both sides are 0, tilt 0, it returns 2.
  3. Node 3 is a leaf: tilt 0, it returns 3.
  4. Back at node 1: left = 2, right = 3, tilt |2 - 3| = 1, total = 1. It returns 6.
  5. Every node is done, so the answer is 1.

Code: Python, Java, C++

1class Solution:
2    def findTilt(self, root: Optional[TreeNode]) -> int:
3        self.total = 0
4        def subtree_sum(node):
5            if not node:
6                return 0
7            left = subtree_sum(node.left)
8            right = subtree_sum(node.right)
9            self.total += abs(left - right)
10            return left + right + node.val
11        subtree_sum(root)
12        return self.total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each node is entered once and does constant work after its children return.

Space

O(h)

The recursion stack holds one frame per level of the current path, which is O(log n) for a balanced tree and O(n) for a chain.

Other approaches

Recompute sums per node (time O(n^2), space O(h))

For every node, walk its left and right subtrees to sum them. Simple, but nodes deep in the tree get summed once per ancestor.

Iterative post-order with a map (time O(n), space O(n))

Use an explicit stack for post-order and store each node's subtree sum in a hash map. Avoids recursion depth limits on very deep trees at the cost of extra memory.

Common mistakes

  • Using the children's values instead of their whole subtree sums. The tilt compares entire subtrees, not just the two child nodes.
  • Returning the tilt from the helper instead of the subtree sum. The helper must return the sum, and the tilt goes into a separate total.
  • Forgetting that values can be negative, so the difference needs an absolute value.

Frequently asked questions

Why post-order and not pre-order?

A node's tilt depends on its subtrees, so both children must be finished first. Post-order is exactly the order in which children return before their parent.

Do I need a global variable?

No. The helper can return a pair (sum, tilt total) instead. A captured variable or class field is simply shorter.

What is the tilt of a leaf?

Zero. Both of its subtrees are empty and sum to 0.