Introduction
LeetCode 144 – Binary Tree Preorder Traversal is one of the most important beginner-friendly tree traversal problems in Data Structures and Algorithms.
This problem helps you understand:
- Binary Tree Traversal
- Depth First Search (DFS)
- Recursion
- Stack-based traversal
- Tree traversal patterns
Preorder traversal is widely used in:
- Tree copying
- Serialization
- Expression trees
- DFS-based problems
- Hierarchical data processing
It is also one of the most commonly asked tree problems in coding interviews.
Problem Link
🔗 Problem
LeetCode 144: Binary Tree Preorder Traversal
Official Problem:
Problem Statement
Given the root of a binary tree, return the preorder traversal of its nodes' values.
What is Preorder Traversal?
In preorder traversal, nodes are visited in this order:
The root node is processed first before traversing subtrees.
Example
Input
Tree Structure:
Preorder Traversal
Traversal order:
Output:
Recursive Approach (Most Common)
Intuition
In preorder traversal:
- Visit current node
- Traverse left subtree
- Traverse right subtree
This naturally fits recursion because trees themselves are recursive structures.
Recursive DFS Visualization
Traversal pattern:
Recursive function:
Java Recursive Solution
Dry Run – Recursive Approach
Tree:
Step 1
Start at:
Add:
Move right to:
Step 2
Add:
Move left to:
Step 3
Add:
Final Answer
Time Complexity – Recursive
Time Complexity
Every node is visited once.
Space Complexity
Where:
H= height of tree- Recursive call stack uses extra space
Worst case:
for skewed trees.
Iterative Approach (Interview Follow-Up)
The follow-up asks:
Can you solve it iteratively?
Yes.
We use a stack to simulate recursion.
Iterative Preorder Intuition
Preorder traversal order is:
Using a stack:
- Process current node immediately
- Push right child first
- Push left child second
Why?
Because stacks follow:
So left subtree gets processed first.
Stack-Based Iterative Logic
Algorithm
- Push root into stack.
- Pop node.
- Add node value.
- Push right child.
- Push left child.
- Repeat until stack becomes empty.
Java Iterative Solution
Dry Run – Iterative Approach
Tree:
Step 1
Push:
Step 2
Pop:
Add:
Push right child:
Step 3
Pop:
Add:
Push left child:
Step 4
Pop:
Add:
Final Answer
Comparison of Approaches
| Approach | Advantages | Disadvantages |
| Recursive | Easy to understand | Uses recursion stack |
| Iterative | Better interview practice | Slightly harder logic |
Interview Explanation
In interviews, explain:
Preorder traversal processes nodes in Root → Left → Right order. Recursion naturally handles this traversal. Iteratively, we use a stack and push the right child before the left child so the left subtree gets processed first.
This demonstrates strong DFS and stack understanding.
Common Mistakes
1. Wrong Traversal Order
Incorrect:
That is inorder traversal.
Correct preorder:
2. Forgetting Null Base Case
Always check:
3. Wrong Stack Push Order
For iterative traversal:
- Push right first
- Push left second
Otherwise traversal order becomes incorrect.
FAQs
Q1. Why is preorder traversal useful?
It is heavily used in:
- Tree cloning
- Serialization
- DFS traversal
- Expression trees
Q2. Which approach is preferred in interviews?
Recursive is simpler.
Iterative is often asked as a follow-up.
Q3. Can preorder traversal be done without stack or recursion?
Yes.
Using Morris Traversal.
Q4. What is the difference between preorder, inorder, and postorder?
| Traversal | Order |
| Preorder | Root → Left → Right |
| Inorder | Left → Root → Right |
| Postorder | Left → Right → Root |
Bonus: Morris Preorder Traversal
Morris traversal performs preorder traversal using:
extra space.
This is considered an advanced interview topic.
Conclusion
LeetCode 144 is one of the most fundamental binary tree traversal problems.
It teaches:
- DFS traversal
- Recursion
- Stack simulation
- Binary tree fundamentals
The key preorder pattern is:
Mastering this traversal builds a strong foundation for advanced tree problems such as:
- Tree serialization
- DFS-based problems
- Tree reconstruction
- Expression trees
- Morris traversal





