9. Binary Tree Inorder Traversal
easyAsked at InstacartIn-order traverse a binary tree — Instacart uses this to confirm tree fluency before category-hierarchy walking problems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, return the in-order traversal of its nodes' values.
Constraints
The number of nodes in the tree is in the range [0, 100]-100 <= Node.val <= 100
Examples
Example 1
root = [1,null,2,3][1,3,2]Example 2
root = [][]Approaches
1. Recursive DFS
Recurse left, push root, recurse right.
- Time
- O(n)
- Space
- O(h)
const res = [];
function dfs(n) {
if (!n) return;
dfs(n.left); res.push(n.val); dfs(n.right);
}
dfs(root);
return res;Tradeoff:
2. Iterative stack
Walk left pushing onto a stack, then pop and pivot right.
- Time
- O(n)
- Space
- O(h)
function inorder(root) {
const res = [], stack = [];
let cur = root;
while (cur || stack.length) {
while (cur) { stack.push(cur); cur = cur.left; }
cur = stack.pop();
res.push(cur.val);
cur = cur.right;
}
return res;
}Tradeoff:
Instacart-specific tips
Instacart's interviewers may follow up with 'walk the aisle-category tree in display order' — be ready to defend why iterative is preferred for deep production trees.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Binary Tree Inorder Traversal and other Instacart interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →