Skip to main content

9. Binary Tree Inorder Traversal

easyAsked at Activision

Return the inorder traversal of a binary tree — Activision asks this to verify recursion-versus-stack fluency that maps to scene-graph traversal.

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

Problem

Given the root of a binary tree, return its inorder (left, root, right) traversal as a list of values.

Constraints

  • 0 <= nodes <= 100
  • -100 <= Node.val <= 100

Examples

Example 1

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

Example 2

Input
[]
Output
[]

Approaches

1. Recursion

Recurse left, append node, recurse right.

Time
O(n)
Space
O(h)
function inorder(root, out=[]) {
  if (!root) return out;
  inorder(root.left, out);
  out.push(root.val);
  inorder(root.right, out);
  return out;
}

Tradeoff:

2. Iterative with explicit stack

Push lefts until null, pop and visit, then go right. Avoids deep recursion stack overflow.

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:

Activision-specific tips

Activision is partial to the iterative version — it signals you would not blow the stack on deep behavior trees used in their AI authoring tooling.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →