2Brute2Best
πŸ”₯0

Binary Tree Inorder Traversal

EasyΒ·LeetCode #94 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given the root of a binary tree. Return the values of all its nodes in inorder: first everything in the left subtree, then the node itself, then everything in the right subtree.

The recursive version is short. The follow-up asks you to write it iteratively, without recursion.

Input

The root of a binary tree. The tree may be empty.

Output

A list of the node values, in inorder.

Example 1
Input: root = [1, null, 2, 3]
Output: [1, 3, 2]

Node 1 has no left child, so 1 comes first. Node 2 has the left child 3, so 3 comes before 2.

Example 2
Input: root = [1, 2, 3, 4, 5, null, 8, null, null, 6, 7, 9]
Output: [4, 2, 6, 5, 7, 1, 3, 9, 8]

The left side of 1 is 2, with children 4 and 5 (and 5 has children 6 and 7). It gives 4, 2, 6, 5, 7. Then comes 1. The right side is 3 with no left child, and its right child 8 has the left child 9. It gives 3, 9, 8.

Example 3
Input: root = []
Output: []

There are no nodes, so the list is empty.

Constraints
  • β€’ The number of nodes in the tree is in the range [0, 100].
  • β€’ βˆ’100 ≀ Node.val ≀ 100
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œinorder traversal”
β†’ Left side, then the node, then the right side. The node waits for its left side, which is the whole point of this question.
β€œreturn the list of values”
β†’ We need the values in order. The output list is not counted as extra space.
β€œfollow-up: iterative”
β†’ The recursion's call stack has to become a stack we manage (Basic) or threads (Morris).
03

Approach Complexity Ladder

Brute Force β€” recursionO(N)O(H)

Recurse into the left subtree, record the node, then recurse into the right subtree

Better β€” iterative with an explicit stackO(N)O(H)

Do the recursion by hand: walk left and push, pop to visit, then walk right

Optimal β€” Morris traversal (threads)O(N)O(1)

Use empty right pointers as links back up the tree; no stack, O(1) extra space

Brute Force Intuition

Inorder is already a recursive rule: the left part, then this node, then the right part. Writing the rule as a function call is the whole solution. The call stack does the bookkeeping, which is why it is short.

Recognition rule for this sub-pattern:

Visit nodes in pre/in/post order or walk two trees together node by node.
Remember this Β· Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.