What does it solve?
Depth-first traversal walks as deep as possible before backtracking. The three classic orders visit the same nodes but record them at different moments:
Preorder = node, then left, then right (root first). Inorder = left, node, right (sorted order for a BST). Postorder = left, right, then node (children before parent, used for deletion & bottom-up sums). All are O(N), visiting each node once.
The core idea
Complexity
Every node is visited exactly once regardless of order. The recursion tree mirrors the actual tree.
The call stack (or explicit stack) is as deep as the tree height H, up to O(N) for a skewed tree.
Examples & edge cases
Step-by-step table
| Step | action | node | depth | stack | output |
|---|
C++ implementation
My Notes
📝Notes✅
Practice this problem