The pattern: trees
Master binary trees, tree traversals (preorder, inorder, postorder, level order), and Binary Search Tree invariants.
01The tree pattern at a glance
A binary tree is a hierarchical data structure where every node has at most two children: a left child and a right child. Trees appear everywhere in computer science: file systems, DOM trees in browsers, database indices (B-trees), and expression parsers.
In technical interviews, tree problems test your ability to decompose recursive substructures, traverse graphs without cycles, and maintain invariants such as Binary Search Tree order.
1 (root)
/ 2 3
/ \ 4 5 6 (leaves)02The two fundamental traversal paradigms
1. Depth-First Search (DFS)
DFS plunges deep into subtrees before backtracking. Depending on when you visit the current node relative to its children, there are three classic orders:
- Preorder (Node -> Left -> Right): Visit the node first, then traverse the left and right subtrees. Ideal for copying trees, serializing trees, and prefix expressions.
- Inorder (Left -> Node -> Right): Traverse the left subtree, visit the node, then traverse the right subtree. In a Binary Search Tree (BST), inorder traversal produces keys in strictly ascending sorted order!
- Postorder (Left -> Right -> Node): Traverse both children first, then visit the parent. Essential when children must be evaluated before parent decisions can be made: calculating subtree heights, deleting trees, and post-fix expressions.
2. Breadth-First Search (BFS) / Level-Order Traversal
BFS visits nodes horizontal level by horizontal level from top to bottom. Using a FIFO queue (Deque<TreeNode>), you snapshot the queue size at the beginning of each level loop (int size = q.size()), processing an entire depth before advancing to the next.
03Binary Search Tree (BST) invariant
A binary tree is a valid Binary Search Tree (BST) if for every node:
- Every node in its left subtree has a value strictly smaller:
left.val < node.val. - Every node in its right subtree has a value strictly greater:
right.val > node.val. - Both left and right subtrees are also valid BSTs.
A critical trap: checking only node.left.val < node.val < node.right.val is insufficient. The BST property is a global ancestor bound: every node must sit within an open range (low, high) passed down from all its ancestors.
04Tree problem classification
| Category | Primary strategy | Classic problems |
|---|---|---|
| Traversal orders | Explicit stack / recursion | Preorder, Inorder, Postorder, Zigzag |
| Structural properties | Bottom-up postorder | Max Depth, Same Tree, Symmetric Tree |
| Path problems | Top-down passing remainder | Path Sum, Path Sum II, Root-to-Leaf paths |
| Level inspection | FIFO queue with level snapshot | Level Order I & II, Right Side View |
| BST algorithms | Inorder walk / range boundaries | Validate BST, Min Distance, Kth Smallest |
| Construction & Serialization | Preorder bounds / divide-and-conquer | Sorted Array to BST, BST Serialization |