Binary Tree Inorder Traversal
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.
The root of a binary tree. The tree may be empty.
A list of the node values, in inorder.
Node 1 has no left child, so 1 comes first. Node 2 has the left child 3, so 3 comes before 2.
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.
There are no nodes, so the list is empty.
- β’ The number of nodes in the tree is in the range [0, 100].
- β’ β100 β€ Node.val β€ 100
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recurse into the left subtree, record the node, then recurse into the right subtree
Do the recursion by hand: walk left and push, pop to visit, then walk right
Use empty right pointers as links back up the tree; no stack, O(1) extra space
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.