Post Order Processing
or Bottom up recursion
Traverse
563. Binary Tree 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. The tilt of the entire tree is the sum of all node tilts.
- Time
- O(n)
dfsvisits each of thennodes exactly once, accumulatingtilton the way back up.- Space
- O(h)
- The recursion stack goes one frame deep per level, up to the tree height
h.
110. Balanced Binary Tree
Check if a binary tree is height-balanced. A height-balanced tree is one where the left and right subtrees of every node differ in height by no more than 1.
- Time
- O(n)
dfsvisits every node exactly once viadfs(node.left)anddfs(node.right), doingO(1)work per node, so total isO(n), wherenis the number of nodes.- Space
- O(h)
- The recursion stack goes as deep as the tree, holding at most
hframes, wherehis the tree height.
Children Sum in a Binary Tree
Check if a binary tree follows the children sum property. In this property, the sum of values of the left child and right child should be equal to the value of their parent node for all nodes.
- Time
- O(n)
dfsvisits each node exactly once.- Space
- O(h)
- The recursion call stack grows with tree height
h.
1973. Count Nodes Equal to Sum of Descendants
Count the number of nodes whose value is equal to the sum of the values of their descendants. A descendant of a node is any node that is on the path from the node to a leaf.
- Time
- O(n)
dfsvisits each of thennodes exactly once.- Space
- O(h)
- The recursion call stack grows with the tree's height
h.
508. Most Frequent Subtree Sum
Find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).
- Time
- O(n)
nis the number of nodes -dfsvisits every node exactly once.- Space
- O(n)
frequencystores up tondistinct subtree sums.- The recursion stack in
dfsreaches depthh(the tree height), dominated byn.
543. Diameter of Binary Tree
The diameter is the length of the longest path between any two nodes, which may or may not pass through the root. At each node, the longest path through it is left_height + right_height. A single post-order recursion returns each node's height while tracking the best diameter seen so far.
- Time
- O(n)
recursionvisits each of thennodes exactly once, updatingdiameteron the way back up.- Space
- O(h)
- The recursion stack goes one frame deep per level, up to the tree height
h.
366. Find Leaves of Binary Tree
Collect and remove leaves layer by layer until the tree is empty. The key insight is that a node's "collection layer" equals its height (distance to its deepest leaf). A post-order recursion computes each node's height and groups node values by that height, so all nodes removed together share the same height.
- Time
- O(n)
nis the number of nodes -recursionvisits every node exactly once.- Space
- O(n)
hmcollects every node's value exactly once across all height buckets, up tonvalues total, which dominates the recursion stack'sO(h)depth.
250. Count Univalue Subtrees
A univalue subtree is one where every node has the same value. A post-order recursion returns whether the subtree rooted at a node is univalue: it is, when the node matches each existing child and both child subtrees are themselves univalue. Each time that holds, the running count is incremented.
- Time
- O(n)
recursionvisits each of thennodes exactly once.- Space
- O(h)
- The recursion stack holds one frame per level on the current path, where
his the tree height.
2265. Count Nodes Equal to Average of Subtree
- Time
- O(n)
recvisits each of thennodes exactly once.- Space
- O(h)
- The recursion stack holds one frame per level on the current path, where
his the tree height.
1120. Maximum Average Subtree
- Time
- O(n)
recvisits every node exactly once,n= number of nodes in the tree; each call doesO(1)work combining its children's(count, total).- Space
- O(h)
- No structure is allocated beyond the recursion stack, whose depth is the tree height
h(worst casenfor a skewed tree,log nfor a balanced tree).
Manipulate Tree
Transform to Sum Tree
Transform a binary tree into a sum tree where each node contains the sum of left and right subtrees in the original tree.
- Time
- O(n)
dfsvisits each of the tree'snnodes exactly once.- Space
- O(h)
- The recursion call stack grows with the tree's height
h.