Binary Tree Tilt
Easy
Topics
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
null marks a missing child.Input: root = [1,2,3] Output: 1
Example 2
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
- Post-order DFS. A null node returns 0.
- Recurse left and right to get both subtree sums.
- Add
abs(left - right)to the running total. - 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 = 7Note 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
his 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.valin 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.totalimport java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public int findTilt(TreeNode root) {
total = 0;
dfs(root);
return total;
}
int total;
private int dfs(TreeNode node) {
if (node == null) return 0;
int l = dfs(node.left), r = dfs(node.right);
total += Math.abs(l - r);
return node.val + l + r;
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: int findTilt(TreeNode* root) {
int total = 0;
function<int(TreeNode*)> dfs = [&](TreeNode* node) -> int {
if (!node) return 0;
int l = dfs(node->left), r = dfs(node->right);
total += abs(l - r);
return node->val + l + r;
};
dfs(root);
return total;
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {number}
*/ var findTilt = function(root) {
let total = 0;
const dfs = (node) => {
if (!node) return 0;
const l = dfs(node.left), r = dfs(node.right);
total += Math.abs(l - r);
return node.val + l + r;
};
dfs(root);
return total;
};