Walking a tree, parent first
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Walking a tree parent first means visiting each node before any of its children. In binary-tree terminology, this is preorder traversal, and it is useful when the parent node contains information you need to process before deciding how to handle the descendants.
What Parent-First Traversal Means
For each node, the order is:
- visit the current node
- traverse the first child subtree
- traverse the remaining child subtrees
In a binary tree, that usually means “node, left, right.”
Recursive Preorder Traversal
The recursive version is the clearest starting point.
Output:
That output shows the parent is processed before each child subtree.
Why Parent-First Is Useful
Parent-first traversal is natural when the parent node determines context for its descendants. Examples include:
- serializing a tree structure
- copying a tree
- printing directory-like hierarchies
- evaluating or exporting syntax trees in prefix-like form
If your logic depends on setting up state before visiting children, preorder is often the right traversal.
Iterative Version with a Stack
Recursion is elegant, but an explicit stack is useful when you want tighter control or want to avoid deep recursion.
The right child is pushed first so the left child is processed first when popped from the stack.
General Trees with Many Children
For a general tree, parent-first still means visiting the current node before iterating over its children.
This pattern works for menus, organization charts, DOM-like structures, and many other hierarchical datasets.
Compare with Other Traversals
It helps to distinguish preorder from the other common traversals.
- preorder: parent before children
- inorder: left subtree, parent, right subtree
- postorder: children before parent
If the requirement literally says “parent first,” preorder is the intended answer. Choosing inorder or postorder changes the semantic meaning of the walk.
Time and Space Complexity
Parent-first traversal visits each node exactly once, so time complexity is O(n). The extra space is O(h) for recursion or the explicit stack, where h is the tree height. On very unbalanced trees, the recursion depth can approach the number of nodes.
Common Pitfalls
A common mistake is describing preorder traversal correctly but then writing code that prints the parent after recurring into a child. Another is forgetting the base case for None, which causes runtime errors in recursive code. Developers also sometimes push the left child before the right child in the iterative version and then wonder why the output order is reversed. Finally, if the tree can be very deep, the recursive version may hit recursion limits and should be replaced with an explicit stack.
Summary
- Parent-first traversal is preorder traversal.
- In a binary tree, the order is current node, left subtree, right subtree.
- Recursion is the simplest implementation, but a stack-based version is easy too.
- Use parent-first traversal when the parent provides context needed before visiting children.
- Check the visit order carefully, because one misplaced line changes the traversal semantics completely.

