9. Binary Tree Inorder Traversal
easyAsked at DropboxReturn the inorder traversal of a binary tree; Dropbox uses it to probe iterative-tree fluency for walking nested folder hierarchies.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, return the inorder traversal of its nodes' values (left, root, right). Recursive and iterative solutions are both expected.
Constraints
0 <= nodes <= 100-100 <= Node.val <= 100
Examples
Example 1
root=[1,null,2,3][1,3,2]Example 2
root=[][]Approaches
1. Recursive
Visit left subtree, push root.val, visit right subtree.
- Time
- O(n)
- Space
- O(h)
function inorder(node,out=[]) {
if(!node) return out;
inorder(node.left,out);
out.push(node.val);
inorder(node.right,out);
return out;
}Tradeoff:
2. Iterative with stack
Push lefts, pop and emit, then descend right. Equivalent to the explicit call-stack form of recursion.
- Time
- O(n)
- Space
- O(h)
function inorderTraversal(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:
Dropbox-specific tips
Dropbox interviewers often follow up with 'now do it without recursion' — be ready to write the explicit-stack version cleanly, that's where they really grade.
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 Dropbox interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →