Lesson 19 of 30
Two different ways to visit every node in a tree — going deep down one path first, or spreading out level by level — and why neither one is the 'right' one.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Picture a maze. You reach a fork with two paths. One approach: pick a path and follow it as far as it goes — around every corner, down every hallway — until you hit a dead end. Only then do you back up to the fork and try the other path. That's depth-first search: go deep before you go wide.
Now picture dropping a stone into a still pond. The ripple doesn't rush off in one direction. It spreads out evenly, in rings, all at once. That's breadth-first search: it visits everything one step away first, then everything two steps away, then three — ring by ring, level by level.
A tree — the family-tree, folder-structure kind you've already met — is a maze with no loops in it. Depth-first and breadth-first are two different orders for visiting every node in it, nothing more. Neither is more "correct" than the other. They answer different questions.
If "depth-first" or "breadth-first" sound intimidating on their own, here's the whole idea in one line each: depth-first goes deep before it goes wide, and breadth-first goes wide before it goes deep.
const familyTree = {
name: "You",
children: [
{ name: "Parent A", children: [{ name: "Grandparent 1", children: [] }] },
{ name: "Parent B", children: [{ name: "Grandparent 2", children: [] }] },
],
};
// Depth-first: follow one branch all the way down before trying the next
function depthFirst(person) {
console.log(person.name);
for (const child of person.children) {
depthFirst(child); // go all the way down this branch first
}
}
// Breadth-first: visit everyone at the current "ring" before going further out
function breadthFirst(root) {
const queue = [root]; // people waiting to be visited, in order
while (queue.length > 0) {
const person = queue.shift(); // take the next person in line
console.log(person.name);
queue.push(...person.children); // their kids join the back of the line
}
}
depthFirst as a function that calls itself already uses the stack built into how function calls work. That's why the depth-first code above looks shorter than the breadth-first version.Depth-first search is the same self-calling-function idea from recursion in the JavaScript track, now aimed at a tree instead of a single chain of calls. Breadth-first search is new — you'll see the same queue-based pattern again soon, once trees give way to graphs.