Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22
,
1 | 5 |
Return:
1 | [ |
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22
,
1 | 5 |
Return:
1 | [ |
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22
,
1 | 5 |
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
You may return the answer in any order.
Example 1:
1 | Input: n = 4, k = 2 |
Example 2:
1 | Input: n = 1, k = 1 |
Constraints:
1 <= n <= 20
1 <= k <= n
Given an m x n
2d grid
map of '1'
s (land) and '0'
s (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
1 | Input: grid = [ |
Example 2:
1 | Input: grid = [ |
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j]
is '0'
or '1'
.Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
Example 1:
1 | Input: root = [3,9,20,null,null,15,7] |
Example 2:
1 | Input: root = [1,2,2,3,3,null,null,4,4] |
Example 3:
1 | Input: root = [] |
Constraints:
[0, 5000]
.-104 <= Node.val <= 104
Given the head
of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example 1:
1 | Input: head = [-10,-3,0,5,9] |
Example 2:
1 | Input: head = [] |
Example 3:
1 | Input: head = [0] |
Example 4:
1 | Input: head = [1,3] |
Constraints:
head
is in the range [0, 2 * 104]
.-10^5 <= Node.val <= 10^5
Given an integer n
, generate all structurally unique BST’s (binary search trees) that store values 1 … n.
Example:
1 | Input: 3 |
Constraints:
0 <= n <= 8
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
Note:
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Given two arrays, write a function to compute their intersection.
Example 1:
1 | Input: nums1 = [1,2,2,1], nums2 = [2,2] |
Example 2:
1 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] |
Note:
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
1 | struct Node { |
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Follow up:
Example 1:
1 | Input: root = [1,2,3,4,5,6,7] |
Constraints:
4096
.-1000 <= node.val <= 1000