Binary Tree Tilt

Easy

Topics
TreeDFS

The tilt of a node is the absolute difference between the sum of all values in its left subtree and the sum of all values in its right subtree (an empty subtree sums to 0). Return the sum of every node’s tilt.

Example 1

213
largest single tilt
A node’s tilt is the absolute difference between its two subtree sums, and the answer adds up every node’s tilt — 1. One post-order pass returns a subtree sum while accumulating the tilts, so each subtree is summed once.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [1,2,3]
Output: 1

Example 2

325497
largest single tilt
Input:  root = [4,2,9,3,5,null,7]
Output: 15

Constraints

  • 0 <= number of nodes <= 10^4
  • -1000 <= Node.val <= 1000
Editorial — intuition, approach, complexity and pitfalls

Intuition

A node's tilt is the absolute difference between its two subtree sums, and the answer is the sum of every node's tilt. Each node therefore needs two things: its own subtree sum to report upward, and its tilt to add to a global total.

The naive version computes each subtree sum from scratch for every node, re-walking the same nodes repeatedly — O(n^2) on a path-shaped tree. One post-order traversal gives both quantities in a single pass, because a node can compute its sum and its tilt the moment its children have reported theirs.

This is the standard shape for tree problems that need an aggregate: return one value up the recursion, accumulate another on the side. The return value is the subtree sum; the accumulator collects tilt.

Approach

  1. Post-order DFS. A null node returns 0.
  2. Recurse left and right to get both subtree sums.
  3. Add abs(left - right) to the running total.
  4. Return node.val + left + right.

Dry Run

tree      4
         / \
        2   9

leaf 2: children 0,0  tilt |0-0| = 0   returns 2
leaf 9: children 0,0  tilt |0-0| = 0   returns 9
root 4: children 2,9  tilt |2-9| = 7   returns 4+2+9 = 15

total tilt = 0 + 0 + 7 = 7

Note the root's return value (15) is never used by the answer — it exists only so a parent could use it. The answer lives entirely in the accumulator.

Complexity

  • Time — O(n): each node visited once, constant work per node. Against O(n^2) for recomputing subtree sums per node.
  • Space — O(h): the recursion stack, where h is the height — O(log n) balanced, O(n) on a degenerate path. With up to 10^4 nodes, a one-sided tree means 10^4 frames, which is fine in Java and C++ but approaches Python's default recursion limit.

Common Pitfalls

  • Returning the tilt instead of the subtree sum. The parent needs the sum; the tilt goes to the accumulator.
  • Forgetting node.val in the returned sum. The subtree sum includes the node itself.
  • Omitting the absolute value. Tilt is a magnitude, and signed differences cancel across the tree.
  • Accumulating in a local variable that each frame re-initialises. The total has to outlive the recursion — an instance field, a closure variable, or a value threaded through the return.
  • Handling an empty tree as an error. Zero nodes is valid input here and the answer is 0, which the null-returns-0 base case already gives.

Full Solution — all languages

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):
        self.total = 0
        def dfs(node):
            if not node: return 0
            l = dfs(node.left); r = dfs(node.right)
            self.total += abs(l - r)
            return node.val + l + r
        dfs(root)
        return self.total
SensAI
Beta
Tap the mic to stop voice mode