LeetCode ยท #94

Binary Tree Inorder Traversal Solution

Given the root of a binary tree, return its inorder traversal โ€” left, root, right.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Easy

๐Ÿ”—

LeetCode

Problem #94

๐Ÿ—๏ธ

Pattern

Traversal โ€” left โ†’ root โ†’ right

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

Example
Input: 1 \ 2 / 3 Output: [1, 3, 2]
Constraints
โ€ข 0 โ‰ค number of nodes โ‰ค 100 โ€ข -100 โ‰ค Node.val โ‰ค 100
02
Section Two ยท Approach 1

Recursive โ€” O(n)

The simplest approach: recurse left, add current value, recurse right. Clean but uses O(h) call stack space.

Java โ€” Recursive
class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); inorder(root, result); return result; } private void inorder(TreeNode node, List<Integer> res) { if (node == null) return; inorder(node.left, res); res.add(node.val); inorder(node.right, res); } }
Python โ€” Recursive
class Solution: def inorderTraversal(self, root): result = [] def inorder(node): if not node: return inorder(node.left) result.append(node.val) inorder(node.right) inorder(root) return result
03
Section Three ยท Approach 2

Iterative with Stack โ€” O(n)

Simulate the call stack explicitly. Push all left children onto the stack, pop one (it's the "current"), process it, then move to its right child and repeat.

๐Ÿ’ก Mental model: Go as far left as possible, stacking every node you pass. When you can't go further left, pop the top (leftmost unvisited), record it, then step right and repeat. The stack remembers where to backtrack to.
Algorithm
  • Step 1: stack = [], curr = root.
  • Step 2: While curr โ‰  null or stack not empty:
  • Push all left descendants: while curr not null โ†’ push, go left.
  • Pop โ†’ record value โ†’ move to right child.
๐ŸŽฏ Why learn iterative?:
  • The follow-up asks for O(1) space (Morris traversal).
  • The iterative approach is the stepping stone โ€” it makes the recursion explicit, helping you see where Morris eliminates the stack entirely.
04
Section Four ยท Trace

Visual Walkthrough

Iterative inorder โ€” tree [1,null,2,3]
TREE 1 2 3 โ‘  โ‘ก โ‘ข ITERATIVE STACK TRACE โ‘  curr=1 push 1, go left โ†’ null. Pop 1 โ†’ output [1] stack: [] curr=1.right=2 push 2, go left=3 โ‘ก push 3 go left โ†’ null. Pop 3 โ†’ output [1, 3] stack: [2] โ‘ข curr=3.right=null Pop 2 โ†’ output [1, 3, 2]. Done! stack: [] Answer: [1, 3, 2] โœ“
05
Section Five ยท Implementation

Code โ€” Java & Python (Iterative)

Java โ€” Iterative Stack
import java.util.*; class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); Deque<TreeNode> stack = new ArrayDeque<>(); TreeNode curr = root; while (curr != null || !stack.isEmpty()) { while (curr != null) { // push all lefts stack.push(curr); curr = curr.left; } curr = stack.pop(); // visit result.add(curr.val); curr = curr.right; // go right } return result; } }
Python โ€” Iterative Stack
class Solution: def inorderTraversal(self, root): result, stack, curr = [], [], root while curr or stack: while curr: stack.append(curr) curr = curr.left curr = stack.pop() result.append(curr.val) curr = curr.right return result
06
Section Six ยท Analysis

Complexity Analysis

ApproachTimeSpace
RecursiveO(n)O(h) call stack
Iterative stackO(n)O(h) explicit stack
Morris traversalO(n)O(1)
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
EmptynullReturn []. Outer while skips.
Left-skewed3โ†’2โ†’1Push all three, then pop in order: [1,2,3].
Right-skewed1โ†’2โ†’3Each node pops immediately, then goes right: [1,2,3].
โš  Common Mistake: Using only !stack.isEmpty() as the loop condition. When we pop a node and move to its right child, the stack may be empty but curr is non-null โ€” we still have work. Both conditions are needed: curr != null || !stack.isEmpty().

โ† Back to Trees problems