Introduction
LeetCode 94 – Binary Tree Inorder Traversal is one of the most important beginner-friendly tree problems in Data Structures and Algorithms.
This problem helps you understand:
- Binary tree traversal
- Depth First Search (DFS)
- Recursion
- Stack-based traversal
- Tree interview fundamentals
It is commonly asked in coding interviews because tree traversal forms the foundation of many advanced tree problems.
Problem Link
🔗 Problem
LeetCode 94: Binary Tree Inorder Traversal
Official Problem:
Problem Statement
Given the root of a binary tree, return the inorder traversal of its nodes' values.
What is Inorder Traversal?
In inorder traversal, we visit nodes in this order:
Example
Input
Tree Structure:
Inorder Traversal
Step-by-step:
Output:
Recursive Approach (Most Common)
Intuition
In inorder traversal:
- Traverse left subtree
- Visit current node
- Traverse right subtree
This naturally fits recursion because trees themselves are recursive structures.
Recursive DFS Visualization
Traversal order:
Recursive function:
Java Recursive Solution
Dry Run – Recursive Approach
Tree:
Step 1
Start at:
Move left:
Return back.
Add:
Step 2
Move right to:
Move left to:
Add:
Return back.
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 Inorder Intuition
The recursive order is:
So iteratively:
- Keep pushing left nodes into stack
- Process current node
- Move to right subtree
Stack-Based Traversal Logic
Algorithm
While current node exists OR stack is not empty:
- Push all left nodes
- Pop top node
- Add node value
- Move to right subtree
Java Iterative Solution
Dry Run – Iterative Approach
Tree:
Step 1
Push:
Stack:
Step 2
Pop:
Add:
Move right to:
Step 3
Push:
Stack:
Step 4
Pop:
Add:
Step 5
Pop:
Add:
Final Answer
Comparison of Approaches
| Approach | Advantages | Disadvantages |
| Recursive | Easy to write and understand | Uses recursion stack |
| Iterative | Better interview practice | Slightly harder logic |
Interview Explanation
In interviews, explain:
In inorder traversal, we process nodes in Left → Root → Right order. Recursion naturally fits this traversal. For iterative traversal, we use a stack to simulate recursive calls.
This demonstrates strong tree traversal understanding.
Common Mistakes
1. Wrong Traversal Order
Incorrect:
That is preorder traversal.
Correct inorder:
2. Forgetting Null Base Case
Always check:
3. Stack Handling Errors
In iterative traversal:
- Push all left nodes first
- Then process node
- Then move right
FAQs
Q1. Why is inorder traversal important?
It is heavily used in:
- Binary Search Trees
- Expression trees
- Tree reconstruction problems
Q2. What is the inorder traversal of a BST?
It produces values in sorted order.
Q3. Which approach is better for interviews?
Recursive is easier.
Iterative is preferred for deeper interview rounds.
Q4. Can inorder traversal be done without stack or recursion?
Yes.
Using Morris Traversal with:
space.
Bonus: Morris Traversal (Advanced)
Morris Traversal performs inorder traversal without recursion or stack.
Complexity
Time Complexity
Space Complexity
This is an advanced interview optimization.
Conclusion
LeetCode 94 is one of the most fundamental tree traversal problems.
It teaches:
- DFS traversal
- Recursion
- Stack simulation
- Binary tree fundamentals
The key inorder pattern is:
Mastering this problem builds a strong foundation for advanced tree interview questions like:
- BST validation
- Tree iterators
- Tree reconstruction
- Morris traversal
- Kth smallest in BST





