🌳 Trees & Graphs Problems
Master binary trees, BST, graph traversal, topological sort, and tree/graph algorithms
Return the level order traversal of a binary tree (values by level from left to right).
java
Determine if a binary tree is a valid BST.
java
Find the lowest common ancestor (LCA) of two nodes in a binary tree.
java
Count the number of islands in a 2D grid (1 = land, 0 = water).
java
Determine if you can finish all courses given prerequisites (detect cycle in DAG).
java
Return a deep copy of a connected undirected graph.
java
Find the maximum path sum in a binary tree. Path can start and end at any node.
java
Design an algorithm to serialize and deserialize a binary tree.
java
Trees & Graphs Patterns
- ✓ DFS (Depth-First): Recursion or stack, preorder/inorder/postorder
- ✓ BFS (Breadth-First): Queue, level-order traversal, shortest path
- ✓ Topological Sort: Kahn's algorithm or DFS for DAG ordering
- ✓ Union-Find: Connected components, cycle detection in undirected graphs
- ✓ Binary Search Tree: Inorder gives sorted order, O(h) operations
- ✓ Graph coloring: Cycle detection, bipartite check