This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 2: Structures
Trust the recursion: solve it for the children, combine at the parent.
Builds on Stacks.
The question to ask
If my children already told me their answers, how would I compute mine?
Nearly every binary tree question is a recursive function that asks each subtree for something (its height, whether it's balanced, its best path) and combines the two answers at the node. Decide first what the function returns for a subtree, then what it does with its children's answers, then the base case for null. If information flows down instead (a running sum, the max so far on the path), pass it as a parameter. Some questions need both: return one thing upward while updating a global best.
Want to watch it run first? Step through the interactive walkthrough.
Time O(n) · Space O(h) call stack, where h is the height
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Path Sum: the remaining target travels down. A leaf answers the question; every other node delegates to its children.
function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
if (!root) return false;
const remaining = targetSum - root.val;
if (!root.left && !root.right) return remaining === 0;
return hasPathSum(root.left, remaining) || hasPathSum(root.right, remaining);
}Lowest Common Ancestor: a subtree returns p or q if it contains one, else null. The first node where both sides return something is the answer; otherwise pass up whichever side found one.
function lowestCommonAncestor(root: TreeNode | null, p: number, q: number): TreeNode | null {
if (!root || root.val === p || root.val === q) return root;
const left = lowestCommonAncestor(root.left, p, q);
const right = lowestCommonAncestor(root.right, p, q);
if (left && right) return root;
return left ?? right;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given the root of a binary tree, return its maximum depth: the number of nodes on the longest root-to-leaf path.
"Depth" is 1 + max(left, right). The one-line recursion every other tree question builds on.
Not attempted yet
Given the root of a binary tree, invert it so it becomes its mirror image, and return the root.
"Invert": swap the two children, recurse into both. Pre-order or post-order both work.
Not attempted yet
Given the roots of two binary trees, check whether they are the same tree: identical structure and node values.
"Same tree": both null is true, one null is false, otherwise values match and both pairs of children match.
Not attempted yet
Return the diameter of a binary tree: the number of edges on the longest path between any two nodes, which may not pass through the root.
"Diameter": return height upward, update a global best with left + right at every node.
Not attempted yet
Determine whether a binary tree is height-balanced: at every node the subtree heights differ by at most one.
"Balanced" needs heights from below: return -1 as soon as any subtree is unbalanced, so it stays O(n).
Not attempted yet
Given two binary trees, return true if the second appears as a subtree of the first, with the same structure and values.
"Subtree": run same-tree from every node of the big tree. O(m × n), and say so.
Not attempted yet
Return true if the binary tree has a root-to-leaf path whose values add up to targetSum.
"Root-to-leaf" "path sum": pass the remaining target down, check it at leaves.
Not attempted yet
Given a binary tree and two of its nodes p and q, return their lowest common ancestor.
"Lowest common ancestor" in a general tree: each subtree reports whether it found p or q; the node where both sides report is the answer.
Not attempted yet
Build the binary tree from its preorder and inorder traversal arrays. Values are unique.
"Preorder and inorder": preorder's next value is the root, its position in inorder splits the left and right subtrees. A Map of inorder indices makes it O(n).
Not attempted yet
A node is good if no node on the path from the root to it has a greater value. Count the good nodes in the binary tree.
"Good nodes" depend on the path above: pass the running max down as a parameter.
Not attempted yet
A path can start and end at any nodes in the binary tree. Return the maximum path sum of any non-empty path.
"Maximum path sum": return the best one-sided gain (clamped at 0) upward, and update a global with node + left gain + right gain.
Not attempted yet
Design functions to serialize a binary tree to a string and deserialize that string back into the same tree.
"Serialize": preorder with a marker for null is enough to rebuild. Deserialize by consuming tokens in the same order.
Not attempted yet