🌳 Trees & Graphs Problems
Master binary trees, BST, graph traversal, topological sort, and tree/graph algorithms
Binary Tree Level Order Traversal - BFS
MediumReturn the level order traversal of a binary tree (values by level from left to right).
java
Validate Binary Search Tree
MediumDetermine if a binary tree is a valid BST.
java
Lowest Common Ancestor of Binary Tree
MediumFind the lowest common ancestor (LCA) of two nodes in a binary tree.
java
Number of Islands - DFS/BFS Grid Traversal
MediumCount the number of islands in a 2D grid (1 = land, 0 = water).
java
Course Schedule - Detect Cycle in Directed Graph
MediumDetermine if you can finish all courses given prerequisites (detect cycle in DAG).
java
Clone Graph - Deep Copy Graph with Cycles
MediumReturn a deep copy of a connected undirected graph.
java
Binary Tree Maximum Path Sum
HardFind the maximum path sum in a binary tree. Path can start and end at any node.
java
Serialize and Deserialize Binary Tree
HardDesign 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