Skip to main content

10. Binary Tree Inorder Traversal

easyAsked at Expedia

Return the inorder traversal of a binary tree's nodes' 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. Inorder = left, node, right.

Constraints

  • The number of nodes is in [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. Recursion

Recursively visit left, node, right.

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

Tradeoff:

2. Iterative with stack

Walk left pushing nodes; pop, emit, then go right. Expedia uses this when traversing destination-category trees without recursion-depth risk.

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:

Expedia-specific tips

Expedia interviewers tend to ask follow-ups about Morris traversal for O(1) space — knowing it signals strong fundamentals around traversal of large destination ontologies.

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

Practice these live with InterviewChamp.AI →