9. Binary Tree Inorder Traversal
easyAsked at GlassdoorReturn the inorder traversal of a binary tree — Glassdoor uses this to gauge tree recursion fluency, then probes the iterative version.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, return the inorder traversal of its nodes' values. Inorder traversal visits left subtree, then node, then right subtree.
Constraints
The number of nodes is in [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 value, recurse right.
- Time
- O(n)
- Space
- O(h)
function inorder(root, out = []) {
if (!root) return out;
inorder(root.left, out);
out.push(root.val);
inorder(root.right, out);
return out;
}Tradeoff:
2. Iterative with stack
Walk left as far as possible, pop, then move right; explicit stack avoids recursion limits.
- Time
- O(n)
- Space
- O(h)
function inorderIter(root) {
const out = [], stack = [];
let cur = root;
while (cur || stack.length) {
while (cur) { stack.push(cur); cur = cur.left; }
cur = stack.pop();
out.push(cur.val);
cur = cur.right;
}
return out;
}Tradeoff:
Glassdoor-specific tips
Glassdoor often follows the recursive answer with 'now show me iterative' — bonus signal is mentioning Morris traversal for O(1) space, which echoes their tight-memory aggregation jobs.
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 Glassdoor interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →