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

easy
Return every root-to-leaf path in a binary tree as a list of node values, instead of an arrow-joined string.

Binary Tree Basics

easy
An 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

easy
Return every root-to-leaf path in a binary tree as an arrow-joined string.

Binary Tree Traversals: Preorder, Inorder, Postorder

easy
Add 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

easy
Build a height-balanced binary search tree from an array sorted in ascending order.

Invert Binary Tree

easy
Flip a binary tree into its mirror image by swapping every node's left and right children.

Minimum Absolute Difference in a BST

easy
Find the smallest absolute difference between the values of any two distinct nodes in a binary search tree.

Range Sum of a BST

easy
Sum every node value in a binary search tree that falls within a given inclusive range.

Search in a Binary Search Tree

easy
Find the node with a given value in a binary search tree and return the subtree rooted there.

Symmetric Tree

easy
Determine whether a binary tree is a mirror of itself around its center.

Binary Tree Left Side View

medium
Return 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

medium
Print 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

medium
Add 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

medium
There is a binary tree rooted at 0 consisting of n nodes.

Binary Search Trees

Graph Traversal: BFS & DFS

Find if Path Exists in Graph

easy
There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).

Flood Fill

easy
You 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

easy
You 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

medium
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.

Accounts Merge

medium
Given 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

medium
Given 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

medium
You 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

medium
Given 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

medium
Given 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

medium
There 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

medium
There are n cities connected by some number of flights.

Check if There is a Valid Path in a Grid

medium
You are given an m x n grid.

Check Knight Tour Configuration

medium
There is a knight on an n x n chessboard.

Coin Change

medium
You 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

medium
You are given an m x n matrix grid and a positive integer k.

Count Servers that Communicate

medium
You 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

medium
You 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

medium
You are given an integer n.

Count Unreachable Pairs of Nodes in an Undirected Graph

medium
You are given an integer n.

Detect Cycles in 2D Grid

medium
Given 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

medium
You 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

medium
You 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?

medium
There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.

Lexicographically Smallest Equivalent String

medium
You are given two strings of the same length s1 and s2 and a string baseStr.

Longest Consecutive Sequence

medium
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

Make Lexicographically Smallest Array by Swapping Elements

medium
You are given a 0-indexed array of positive integers nums and a positive integer limit.

Max Area of Island

medium
You are given an m x n binary matrix grid.

Maximum Number of Fish in a Grid

medium
You 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

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Course Schedule II

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Course Schedule IV

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Digit Operations to Make Two Integers Equal

medium
You are given two integers n and m that consist of the same number of digits.

Find a Safe Walk Through a Grid

medium
You are given an m x n binary matrix grid and an integer health.

Find All Possible Recipes from Given Supplies

medium
You have information about n different recipes.

Find Eventual Safe States

medium
There is a directed graph of n nodes with each node labeled from 0 to n - 1.

Find Minimum Time to Reach Last Room I

medium
There is a dungeon with n x m rooms arranged as a grid.

Find Minimum Time to Reach Last Room II

medium
There 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

medium
There are n cities numbered from 0 to n-1.

Loud and Rich

medium
There 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

medium
You 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

medium
You 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

medium
You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters.