Binary Tree Preorder Traversal
An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 3
- Core for
- 4 roles
The problem
Collect the values of a binary tree in preorder: for every node, record the node first, then its left subtree, then its right subtree. Return them as a list.
Example 1
- Input
- [5,3,7,1,4,null,8]
- Output
- [5,3,1,4,7,8]
- Why
- Root 5, left 3 (children 1,4), right 7 (right 8). Preorder: 5,3,1,4,7,8.
Example 2
- Input
- []
- Output
- []
- Why
- An empty tree produces an empty traversal.
Example 3
- Input
- [1,2]
- Output
- [1,2]
- Why
- Root 1 is visited first, then its left child 2.
Constraints
- The number of nodes is in the range [0, 100].
- -100 <= Node.val <= 100
How to think about it
Updated 2026-09-29Preorder processes each node the instant your eyes first land on it. Unlike inorder or postorder, no state needs to be suspended waiting for descendants; consumption is immediate, meaning any iterative stack has to remember where to resume next, pushing right before left so left pops first.
Approaches, worst first
Recursive visit
time O(n) · space O(n)
Emit the current node value, recurse on the left child, then recurse on the right. Clean and direct, but retains an implicit stack proportional to tree depth.
Iterative stack with child ordering
time O(n) · space O(n)
Pop the current node, push its right child, then push its left child. Because the stack is last-in-first-out, the left subtree is processed entirely before the right subtree begins.
Morris threadingWrite this one
time O(n) · space O(1)
Connect the inorder predecessor's right pointer to the current node, but emit the value the first time the node is linked rather than when the link is broken. Traversal runs in place without extra space.
Where people lose marks · 3
- Pushing the left child before the right child onto an explicit stack reverses the traversal order to root-right-left.
- Pushing null children into the stack without filtering causes unnecessary pop cycles and potential null dereferences.
- Omitting the empty-tree check at the very top causes an immediate undefined error when pushing root to an iterative stack.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .