binary tree
3
2
5
4
9
7
call stack ↓
(returned)
Post-order DFS (sum up, accumulate tilt)
▸1total = 02sum(node):3 if node is null: return 04 sumL = sum(node.left)5 sumR = sum(node.right)6 total += abs(sumL - sumR) // accumulate tilt7 return node.val + sumL + sumR // subtree sum8// answer = total
state
- tilt(node)|sumL − sumR|
- answerΣ tilt(node)
- total so far0