π³ PHP Trees β Topic Index
A tree is a hierarchical data structure with a root node and subtrees of children. Binary trees and BSTs are the most common in interviews.
Topics Covered
| # | Topic | Key Concepts |
|---|---|---|
| 1 | Basics | Node structure, BST, height, depth |
| 2 | Traversal | DFS (in/pre/post), BFS, level-order |
| 3 | Interview Questions | 25 classic problems |
Why Trees?
- Hierarchical data β file systems, DOM, org charts
- Fast search β BST gives O(log n) search/insert/delete on balanced trees
- Foundation for heaps, tries, segment trees, and graphs
Common Patterns
- DFS (recursive) β inorder, preorder, postorder; most tree problems
- BFS (queue) β level-order traversal; shortest path in trees
- Height/depth recursion β return value bubbles up from leaves
- Two-pass / ancestor tracking β LCA, path sum problems
- BST property β left < root < right; validate, search, insert