Binary Tree Inorder Traversal (Binary Trees)
An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 1
- Core for
- 4 roles
The problem
Return the inorder list of a binary tree's values (left subtree, node, right subtree) using the Morris technique: temporarily point each predecessor's empty right link back to its ancestor instead of keeping a stack, so the extra memory stays O(1) and the tree is restored afterwards.
Example 1
- Input
- [5,2,7,1,3,6,8]
- Output
- [1,2,3,5,6,7,8]
- Why
- Tree structure: root 5, left subtree [2,1,3], right subtree [7,6,8]. Inorder gives sorted 1,2,3,5,6,7,8.
Example 2
- Input
- [8,4,12,2,6,10,14]
- Output
- [2,4,6,8,10,12,14]
- Why
- Balanced BST inorder gives sorted sequence: 2,4,6,8,10,12,14.
Example 3
- Input
- []
- Output
- []
- Why
- An empty tree produces an empty traversal.
Constraints
- The number of nodes is in the range [0, 100].
- -100 <= Node.val <= 100
How to think about it
Updated 2026-09-29Standard traversal needs a stack to return to an ancestor after completing a left subtree. Morris traversal creates that return path directly inside the tree: find the inorder predecessor (the rightmost node of the left subtree) and link its right pointer to the current node as a temporary bridge back.
Approaches, worst first
Iterative stack traversal
time O(n) · space O(h)
Use an explicit stack to push nodes going left, popping and stepping right. While straightforward, it uses memory proportional to tree height, violating the O(1) space constraint.
Morris traversal with temporary threadsWrite this one
time O(n) · space O(1)
For current node, if left child is null, record value and go right. Otherwise find the rightmost node in the left subtree. If its right pointer is null, set it to current and step left. If already pointing to current, cut the thread, record current, and step right.
Where people lose marks · 3
- In the predecessor search loop, failing to check `pred.right !== curr` leads to an infinite loop around the newly created thread.
- Forgetting to restore `pred.right = null` permanently alters the input tree structure.
- Recording the node value when the thread is created instead of when it is destroyed converts the output from inorder to preorder.
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 .