Skip to main content

9. Binary Tree Inorder Traversal

easyAsked at Vercel

Walk a binary tree in-order and return the values. Vercel asks this as a recursion warmup before harder tree problems — and because the iterative version with an explicit stack mirrors how their build graph walks dependencies.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2026-Q1)Vercel platform onsite; the iterative version is the bonus signal.
  • LeetCode Discuss (2025-11)Reported as Vercel screen recursion warm-up.

Problem

Given the root of a binary tree, return the inorder traversal of its nodes' values. Inorder is: left subtree, node, right subtree.

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Examples

Example 1

Input
root = [1,null,2,3]
Output
[1,3,2]

Example 2

Input
root = []
Output
[]

Approaches

1. Recursive DFS

Recurse left, push current, recurse right.

Time
O(n)
Space
O(h) stack
function inorderTraversal(root) {
  const out = [];
  function dfs(node) {
    if (!node) return;
    dfs(node.left);
    out.push(node.val);
    dfs(node.right);
  }
  dfs(root);
  return out;
}

Tradeoff: Simple and correct. The follow-up will ask for iterative — be ready.

2. Iterative with explicit stack (preferred)

Push left children onto a stack until null. Pop, emit, then move to the right subtree and repeat.

Time
O(n)
Space
O(h)
function inorderTraversal(root) {
  const out = [];
  const 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: Avoids recursion stack overflow on skewed trees. The iterative pattern generalizes to Morris (O(1) space) and to any DFS over a tree-shaped dependency graph.

Vercel-specific tips

Vercel will accept the recursive version, then ask for the iterative one. Have both ready. Bonus signal: mentioning Morris traversal (O(1) space, briefly modifies the tree) shows depth even if you don't code it.

Common mistakes

  • Pre-order instead of in-order — pushing the node value BEFORE recursing into left. Easy mistake under pressure.
  • Forgetting the outer `while (cur || stack.length)` — the loop exits early when stack is briefly empty mid-traversal.
  • In the iterative version, reassigning cur to cur.left AFTER pop — instead of cur.right.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Pre-order (LC 144) and Post-order (LC 145) iterative.
  • Morris traversal — O(1) extra space.
  • Validate a BST using inorder (LC 98).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is the iterative version harder than expected?

Because in-order visits the node BETWEEN its subtrees, you need to drill down to the leftmost node, emit it, then 'remember' to come back and handle the right subtree. The stack holds that memory.

When would Morris matter?

When tree height is enormous and stack space is constrained — e.g., a 10M-node skewed tree. Morris uses threaded pointers temporarily but keeps O(1) extra space.

Practice these live with InterviewChamp.AI

Drill Binary Tree Inorder Traversal and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →