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
Process the tree one level at a time.
Builds on Tree DFS.
The question to ask
Is the answer organised by level, or about the nearest node to the root?
When a question talks about levels (each level's values, the rightmost node per level, a zigzag, the width of a level) or about the shallowest something, a queue processes nodes in level order and you can snapshot the queue's length to know where each level ends. BFS also finds the minimum depth without exploring the deep branches DFS would wander into first.
Want to watch it run first? Step through the interactive walkthrough.
Time O(n) · Space O(w), where w is the widest level
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.
Right Side View is level order keeping only the last value per level. Minimum Depth returns the level number at the first leaf.
function rightSideView(root: TreeNode | null): number[] {
const seen: number[] = [];
let frontier: TreeNode[] = root ? [root] : [];
while (frontier.length > 0) {
seen.push(frontier[frontier.length - 1].val);
frontier = frontier.flatMap((node) => [node.left, node.right].filter((c): c is TreeNode => c !== null));
}
return seen;
}
function minDepth(root: TreeNode | null): number {
let depth = 0;
let frontier: TreeNode[] = root ? [root] : [];
while (frontier.length > 0) {
depth++;
if (frontier.some((node) => !node.left && !node.right)) return depth;
frontier = frontier.flatMap((node) => [node.left, node.right].filter((c): c is TreeNode => c !== null));
}
return depth;
}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 the average value of the nodes on each level.
"Average" per "level": level order with a sum per level.
Not attempted yet
Return the minimum depth of a binary tree: the number of nodes on the shortest path from the root down to the nearest leaf.
"Minimum depth" to the nearest leaf: BFS stops at the first leaf it meets, never exploring deeper branches.
Not attempted yet
Return the level order traversal of a binary tree's values: left to right, level by level.
"Level order" is the pattern's name. The template, nothing more.
Not attempted yet
Imagine standing on the right side of a binary tree. Return the values of the nodes you can see, top to bottom.
"Right side view" is the last node of each level. (DFS visiting right first, recording the first node per depth, also works.)
Not attempted yet
Return the zigzag level order traversal: left to right on one level, right to left on the next, alternating.
"Zigzag level order": ordinary level order, reversing every other level's values.
Not attempted yet
Return the maximum width of any level of the binary tree, counting the null positions between the end nodes.
"Width" counting gaps: give each node a heap-style index (2i, 2i + 1) during BFS. Re-base per level so the numbers don't blow up.
Not attempted yet
In a perfect binary tree, set each node's next right pointer to the node to its right on the same level, or null.
"Next right pointer" on each level: link nodes as you walk a level. The O(1) version uses the already-linked level above.
Not attempted yet
Given a binary tree, a target node and k, return the values of all nodes at distance k from the target.
"Distance k" can go up through parents. Record each node's parent, then BFS k steps out from the target as if it were a graph.
Not attempted yet