Iterator over a Binary Search Tree
Implement an iterator over a binary search tree.
Input: root = [7,3,15,null,null,9,20], calls: next(), hasNext(), next()
Output: 3, TRUE, 7
Explanation: The iterator yields the smallest value (3), confirms more nodes exist, then properly yields the next in-order value (7).
Unique Combinations that Sum to a Target
Find all unique combinations in an array that sum to a target.
Input: candidates = [2,4,6], target = 6
Output: [[2,2,2], [2,4], [6]]
Explanation: Uses backtracking to find all valid combinations that sum to 6, allowing for explicitly repeated elements.
Find the shortest distance from all buildings to an empty land in a grid.
You are given a grid of values 0, 1, or 2, where 0 is empty land, 1 is a building, and 2 is an obstacle. Find the shortest travel distance from all buildings to an empty land spot.
Input: grid = [[1,0],[0,1]]
Output: 2
Explanation: The empty lands are at (0,1) a…
Find the longest increasing path in a matrix.
Given an m x n integers matrix, return the length of the longest strictly increasing path in the matrix. From each cell, you can either move in four directions: left, right, up, or down.
Input: matrix = [[1,2],[3,4]]
Output: 3
Explanation: The longest strictly increasing path is 1 -> 3 ->…
Clone an Undirected Graph
Clone an undirected graph.
Input: adjList = [[]]
Output: [[]]
Explanation: Creates a new, deeply cloned graph containing only one single node with zero connected neighbors.
Determine if a Sudoku board is valid.
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to standard Sudoku rules: each row, column, and 3x3 sub-box must contain the digits 1-9 without repetition.
Input: board = 9x9 grid where the first row contains 7 twice
Output: FALSE
Explanation: A val…
Find the median from a continuous data stream.
Implement a data structure that allows you to add integers from a continuous data stream and find the median of all elements so far efficiently.
Input: addNum(2), addNum(3), findMedian(), addNum(4), findMedian()
Output: 2.5 then 3.0
Explanation: After adding 2 and 3, the median is 2.5. Af…
Perform a depth-first search on a matrix to find connected components.
Given an n x n matrix where isConnected[i][j] = 1 if the ith node and the jth node are directly connected, and 0 otherwise, return the total number of connected components.
Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
Explanation: There are no connectio…