9. Binary Tree Inorder Traversal
easyAsked at SlackReturn the inorder traversal of a binary tree's values.
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).
Constraints
Number of nodes in [0, 100]-100 <= Node.val <= 100
Examples
Example 1
root = [1,null,2,3][1,3,2]Example 2
root = [][]Approaches
1. Recursive
Simple recursion: left, push value, right.
- Time
- O(n)
- Space
- O(h)
const out=[];
function dfs(n){ if(!n) return; dfs(n.left); out.push(n.val); dfs(n.right); }
dfs(root); return out;Tradeoff:
2. Iterative with stack
Push lefts onto a stack, then pop, record, and move right. Handles deep trees without recursion limit.
- 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:
Slack-specific tips
Slack channel-tree (workspaces > channels > threads) is a real recursive structure — they may ask you to convert recursion to iteration to avoid stack overflow on huge workspaces.
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 Slack interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →