#94

Binary Tree Inorder Traversal

Easy Iterative inorder with a stack

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the values of a binary tree in inorder (left subtree, node, right subtree).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: list nodes as left subtree, node, right subtree.

Why this works: the iterative version pushes all left children, pops one, visits it, then moves to its right child.

Example: 1 with right child 2, and 2 has left child 3

go left from 1: none → visit 1go to 2 → left 3 → visit 3, visit 2[1, 3, 2]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Visiting the node when it's pushed — that's preorder, not inorder.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(h), h = tree height.
Topic: Binary Trees
🎬 Episodes 53–60: video coming soon