Skip to main content

9. Binary Tree Inorder Traversal

easyAsked at Postman

Return 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. Iterative and recursive forms are both expected.

Constraints

  • 0 <= number of nodes <= 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

Recurse left, visit, 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 stack

Push left-spine onto stack, pop and emit, then walk right. Constant stack depth bounded by tree height.

Time
O(n)
Space
O(h)
function inorder(root) {
  const out = [], st = [];
  let curr = root;
  while (curr || st.length) {
    while (curr) { st.push(curr); curr = curr.left; }
    curr = st.pop();
    out.push(curr.val);
    curr = curr.right;
  }
  return out;
}

Tradeoff:

Postman-specific tips

Postman uses tree traversal patterns to walk Collection trees (folders containing folders of requests), so the iterative stack form maps directly to their UI render code.

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 Postman interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →