Graphs & Trees Deep Dive
70 questions across trees and graphs — traversals, BSTs, BFS/DFS, union-find, and the advanced graph algorithms that show up in senior-level interviews.
Tree Traversals & Recursion
Get All Root-to-Leaf Paths
easyReturn every root-to-leaf path in a binary tree as a list of node values, instead of an arrow-joined string.
Binary Tree Basics
easyAn introduction to the Node and BinTree building blocks used throughout the Binary Trees series, plus a sample tree these exercises build on.
Binary Tree Paths
easyReturn every root-to-leaf path in a binary tree as an arrow-joined string.
Binary Tree Traversals: Preorder, Inorder, Postorder
easyAdd preorder, inorder, and postorder traversal methods to BinTree and see how each visits the same sample tree in a different order.
Convert a Sorted Array to a Balanced BST
easyBuild a height-balanced binary search tree from an array sorted in ascending order.
Invert Binary Tree
easyFlip a binary tree into its mirror image by swapping every node's left and right children.
Minimum Absolute Difference in a BST
easyFind the smallest absolute difference between the values of any two distinct nodes in a binary search tree.
Range Sum of a BST
easySum every node value in a binary search tree that falls within a given inclusive range.
Search in a Binary Search Tree
easyFind the node with a given value in a binary search tree and return the subtree rooted there.
Symmetric Tree
easyDetermine whether a binary tree is a mirror of itself around its center.
Binary Tree Left Side View
mediumReturn the value of the leftmost node visible at each level of a binary tree, from top to bottom.
Print Left View of a Binary Tree
mediumPrint the first node visible at each level of the sample tree, extending level order traversal with a single boolean flag.
Binary Tree Level Order Traversal
mediumAdd a queue-based level order (breadth-first) traversal to BinTree, visiting the sample tree one level at a time.
Count Nodes With the Highest Score
mediumThere is a binary tree rooted at 0 consisting of n nodes.
Binary Search Trees
Delete a Node in a Binary Search Tree
mediumRemove the node with a given value from a binary search tree while preserving its ordering property.
Insert into a Binary Search Tree
mediumInsert a new value into a binary search tree while preserving its ordering property.
Kth Smallest Element in a BST
mediumFind the k-th smallest value stored in a binary search tree.
Lowest Common Ancestor in a BST
mediumFind the lowest common ancestor of two given values in a binary search tree, using the tree's ordering to avoid a generic tree search.
Unique Binary Search Trees
mediumGiven an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.
Validate a Binary Search Tree
mediumDetermine whether a binary tree satisfies the binary search tree property at every node, not just between direct parent-child pairs.
Number of Ways to Reorder Array to Get Same BST
hardGiven an array nums that represents a permutation of integers from 1 to n.
Graph Traversal: BFS & DFS
Find if Path Exists in Graph
easyThere is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).
Flood Fill
easyYou are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image.
Island Perimeter
easyYou are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
01 Matrix
mediumGiven an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
Accounts Merge
mediumGiven a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails...
All Paths From Source to Target
mediumGiven a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.
Array Nesting
mediumYou are given an integer array nums of length n where nums is a permutation of the numbers in the range [0, n - 1].
As Far from Land as Possible
mediumGiven an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized,...
Battleships in a Board
mediumGiven an m x n matrix board where each cell is a battleship 'X' or empty '.', return the number of the battleships on board.
Bulb Switcher II
mediumThere is a room with n bulbs labeled from 1 to n that all are turned on initially, and four buttons on the wall.
Cheapest Flights Within K Stops
mediumThere are n cities connected by some number of flights.
Check if There is a Valid Path in a Grid
mediumYou are given an m x n grid.
Check Knight Tour Configuration
mediumThere is a knight on an n x n chessboard.
Coin Change
mediumYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Union-Find & Minimum Spanning Trees
Count Islands With Total Value Divisible by K
mediumYou are given an m x n matrix grid and a positive integer k.
Count Servers that Communicate
mediumYou are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server.
Count Sub Islands
mediumYou are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land).
Count the Number of Complete Components
mediumYou are given an integer n.
Count Unreachable Pairs of Nodes in an Undirected Graph
mediumYou are given an integer n.
Detect Cycles in 2D Grid
mediumGiven a 2D array of characters grid of size m x n, you need to find if there exists any cycle consisting of the same value in grid.
Evaluate Division
mediumYou are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i].
Find the Safest Path in a Grid
mediumYou are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: You are initially positioned at cell (0, 0).
Is Graph Bipartite?
mediumThere is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.
Lexicographically Smallest Equivalent String
mediumYou are given two strings of the same length s1 and s2 and a string baseStr.
Longest Consecutive Sequence
mediumGiven an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
Make Lexicographically Smallest Array by Swapping Elements
mediumYou are given a 0-indexed array of positive integers nums and a positive integer limit.
Max Area of Island
mediumYou are given an m x n binary matrix grid.
Maximum Number of Fish in a Grid
mediumYou are given a 0-indexed 2D matrix grid of size m x n, where (r, c) represents: A fisher can start at any water cell (r, c) and can do the following operations any number of...
Advanced Graph Algorithms
Course Schedule
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Course Schedule II
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Course Schedule IV
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Digit Operations to Make Two Integers Equal
mediumYou are given two integers n and m that consist of the same number of digits.
Find a Safe Walk Through a Grid
mediumYou are given an m x n binary matrix grid and an integer health.
Find All Possible Recipes from Given Supplies
mediumYou have information about n different recipes.
Find Eventual Safe States
mediumThere is a directed graph of n nodes with each node labeled from 0 to n - 1.
Find Minimum Time to Reach Last Room I
mediumThere is a dungeon with n x m rooms arranged as a grid.
Find Minimum Time to Reach Last Room II
mediumThere is a dungeon with n x m rooms arranged as a grid.
Find the City With the Smallest Number of Neighbors at a Threshold Distance
mediumThere are n cities numbered from 0 to n-1.
Loud and Rich
mediumThere is a group of n people labeled from 0 to n - 1 where each person has a different amount of money and a different level of quietness.
Minimize the Maximum Edge Weight of Graph
mediumYou are given two integers, n and threshold, as well as a directed weighted graph of n nodes numbered from 0 to n - 1.
Minimum Cost of a Path With Special Roads
mediumYou are given an array start where start = [startX, startY] represents your initial position (startX, startY) in a 2D space.
Minimum Cost to Convert String I
mediumYou are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters.