Binary Tree Inorder Traversal

easy

Given the root of a binary tree, return the inorder traversal of its nodes' values — visit the left subtree, then the node, then the right subtree.

The tree is given in level-order (breadth-first), using null for missing children.

Hints

Inorder means left subtree, then the node, then right subtree.
Recursively that's three lines; iteratively use an explicit stack.
For the iterative version, walk left pushing nodes, then pop to visit and go right.

Common doubts

On a binary search tree, inorder yields the node values in sorted order, which is why many BST problems start with an inorder walk.
Inorder visits the leftmost node first, so you must descend all the way left (stacking nodes) before recording anything.
O(height) for the recursion/stack — O(log n) for a balanced tree, O(n) for a degenerate (list-like) one.

Interview follow-ups

Morris traversal threads temporary links from each subtree's rightmost node back to its successor, achieving O(1) space at the cost of temporarily mutating the tree.
Only in when the node is recorded: preorder records before recursing, postorder records after both children.

Fun facts

  • Inorder traversal of a BST is the basis of the 'kth smallest element' and 'validate BST' solutions.
  • Morris traversal's threading trick briefly turns the tree into a temporary linked structure to avoid a stack entirely.

Asked at

AmazonMicrosoftGoogle
Frequently Sometimes Occasionally
Example 1
Input: root = [1, null, 2, 3]
Output: [1, 3, 2]
Inorder visits the left subtree (empty), then 1, then its right subtree yielding 3 then 2.
Example 2
Input: root = []
Output: []
An empty tree has no nodes.
Constraints

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

Solve this problem →