10. Binary Tree Inorder Traversal
easyAsked at WixReturn the inorder traversal of a binary tree's values; Wix uses the pattern to walk a page's nested component tree in render order.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Constraints
0 <= number of nodes <= 100-100 <= Node.val <= 100
Examples
Example 1
root=[1,null,2,3][1,3,2]Example 2
root=[][]Approaches
1. Recursive
Recurse left, visit, recurse right.
- Time
- O(n)
- Space
- O(h)
function inorderTraversal(root,out=[]){
if(!root) return out;
inorderTraversal(root.left,out);
out.push(root.val);
inorderTraversal(root.right,out);
return out;
}Tradeoff:
2. Iterative stack
Use an explicit stack to walk left, then pop and go right.
- Time
- O(n)
- Space
- O(h)
function inorderTraversal(root){
const out=[],st=[]; let cur=root;
while(cur||st.length){
while(cur){st.push(cur);cur=cur.left;}
cur=st.pop(); out.push(cur.val); cur=cur.right;
}
return out;
}Tradeoff:
Wix-specific tips
Wix often follows up by asking how you'd avoid stack overflow on deep component trees — mention the iterative approach for builder hierarchies that nest 50+ deep.
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 Wix interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →