Inorder Traversal
Visit nodes in Left → Root → Right order. For a BST, this gives you a sorted sequence.
Key concepts at a glance — for those who already know the basics.
Problem Statement
Binary Tree Inorder Traversal
Input: root = [1, null, 2, 3]
Output: [1, 3, 2]
Tree: 1 → right child 2, left child of 2 is 3.
Input: root = [1, 2, 3, 4, 5, 6, 7]
Output: [4, 2, 5, 1, 6, 3, 7]
Perfect binary tree — left subtree, root, right subtree.
Constraints
- The number of nodes in the tree is in the range [0, 100].
- Each node's value is in the range [-100, 100].
The Task
Given the root of a binary tree, return a list of all node values in inorder sequence.
The Rule
Left → Root → Right
At every node: go as far left as possible, visit the current node, then explore the right subtree.
The Intuition
Think of a bookshelf — books sorted left to right. You always finish the left shelf before reading the current book, then move right.
Inorder of a Binary Search Tree always produces values in ascending sorted order.
Mental Model
Go Left
Keep going left until you hit None — the recursion dives to the leftmost leaf.
Visit Current
Only after the entire left subtree is done, append this node's value.
Go Right
The right subtree is a new problem — apply the same Left → Root → Right rule.
Dry Run
Code
Complexity
Time
O(n)
Every node visited exactly once.
Space
O(h)
Recursion stack = tree height. Balanced: log n. Skewed: n.
Interview Questions
Kth smallest in BST
Inorder gives sorted order, so the kth element is the answer.
Validate a BST
Check that the inorder sequence is strictly increasing.
Convert BST to sorted array
Directly collect inorder result.
Recover BST
Find the two swapped nodes by spotting where inorder order breaks.
Ready to see it in action?
Step through the visualizer to watch the algorithm state update live.