Binary Tree Inorder 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 inorder: for every node, first everything in its left subtree, then the node itself, then everything in its right subtree. Return them as a list.
Example 1
- Input
- [2,1,3]
- Output
- [1,2,3]
- Why
- Inorder of 2 with left 1 and right 3 is 1,2,3.
Example 2
- Input
- []
- Output
- []
- Why
- An empty tree has no nodes to traverse.
Example 3
- Input
- [1]
- Output
- [1]
- Why
- A single node tree returns just that node's value.
Constraints
- The number of nodes is in the range [0, 100].
- -100 <= Node.val <= 100
How to think about it
Updated 2026-09-29Inorder traversal is fundamentally about deferring every node until its left descendants have completely spoken. A node cannot be consumed when first seen; it must wait on a shelf until the path deeper to the left hits null, making a last-in-first-out stack the natural physical model of the call stack.
Approaches, worst first
Direct recursion
time O(n) · space O(n)
Recurse left, append the current node value, then recurse right. The recursion stack mirrors the traversal order automatically, though a skewed tree consumes call stack frames proportional to the number of nodes.
Explicit pointer stack
time O(n) · space O(n)
Push nodes while driving a current pointer left until null. Pop to record the top node, then shift current to its right child. It makes the call frame pause and resume explicit without relying on system stack limits.
Morris threadingWrite this one
time O(n) · space O(1)
Find the inorder predecessor in the left subtree and create a temporary thread to the current node. Visiting follows the thread back up and cleans it before stepping right, yielding tree traversal without auxiliary memory.
Where people lose marks · 3
- Returning null instead of an empty list when the root is null; an empty tree has zero nodes to visit.
- Loop termination condition must check both `curr !== null` and `stack.length > 0`; after popping the root, `stack` is empty but `curr` still points to the right child.
- In Morris traversal, forgetting to sever the thread when encountering the predecessor a second time creates an infinite cycle.
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 .