10. Binary Tree Inorder Traversal
easyAsked at UdemyWalk a binary tree in inorder — Udemy uses this as a stack-recursion warm-up before harder course-category tree problems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, return the inorder (left, root, right) traversal of its node values.
Constraints
0 <= nodes <= 100-100 <= Node.val <= 100
Examples
Example 1
root=[1,null,2,3][1,3,2]Example 2
root=[][]Approaches
1. Recursion
Recurse left, push the node, recurse right.
- 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 stack
Push left chain onto a stack; pop, record value, pivot to right child. Continues until both the stack and pointer are empty.
- 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:
Udemy-specific tips
Udemy interviewers value candidates who can speak to recursion vs iteration trade-offs in deeply nested course-category trees that may overflow the call stack.
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 Udemy interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →