2022-03-29
287. Find the Duplicate Number
Topic: Array, Two Pointers, Binary Search, Bit Manipulation
Difficulty: Medium
Problem:
Given an array of integers
There is only one repeated number in
You must solve the problem without modifying the array
Example 1:
Example 2:
Constraints:
•
•
•
• All the integers in
Follow up:
• How can we prove that at least one duplicate number must exist in
• Can you solve the problem in linear runtime complexity?
287. Find the Duplicate Number
Topic: Array, Two Pointers, Binary Search, Bit Manipulation
Difficulty: Medium
Problem:
Given an array of integers
nums containing n + 1 integers where each integer is in the range [1, n] inclusive.There is only one repeated number in
nums, return this repeated number.You must solve the problem without modifying the array
nums and uses only constant extra space.Example 1:
Input: nums = [1,3,4,2,2]
Output: 2
Example 2:
Input: nums = [3,1,3,4,2]
Output: 3
Constraints:
•
1 <= n <= 10^5•
nums.length == n + 1•
1 <= nums[i] <= n• All the integers in
nums appear only once except for precisely one integer which appears two or more times.Follow up:
• How can we prove that at least one duplicate number must exist in
nums?• Can you solve the problem in linear runtime complexity?
2022-03-30
74. Search a 2D Matrix
Topic: Array, Binary Search, Matrix
Difficulty: Medium
Problem:
Write an efficient algorithm that searches for a value
• Integers in each row are sorted from left to right.
• The first integer of each row is greater than the last integer of the previous row.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg
Constraints:
•
•
•
•
74. Search a 2D Matrix
Topic: Array, Binary Search, Matrix
Difficulty: Medium
Problem:
Write an efficient algorithm that searches for a value
target in an m x n integer matrix matrix. This matrix has the following properties:• Integers in each row are sorted from left to right.
• The first integer of each row is greater than the last integer of the previous row.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat.jpg
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= m, n <= 100•
-10^4 <= matrix[i][j], target <= 10^42022-03-31
410. Split Array Largest Sum
Topic: Array, Binary Search, Dynamic Programming, Greedy
Difficulty: Hard
Problem:
Given an array
Write an algorithm to minimize the largest sum among these
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
410. Split Array Largest Sum
Topic: Array, Binary Search, Dynamic Programming, Greedy
Difficulty: Hard
Problem:
Given an array
nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays.Write an algorithm to minimize the largest sum among these
m subarrays.Example 1:
Input: nums = [7,2,5,10,8], m = 2
Output: 18
Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.
Example 2:
Input: nums = [1,2,3,4,5], m = 2
Output: 9
Example 3:
Input: nums = [1,4,4], m = 3
Output: 4
Constraints:
•
1 <= nums.length <= 1000•
0 <= nums[i] <= 10^6•
1 <= m <= min(50, nums.length)2022-04-01
344. Reverse String
Topic: Two Pointers, String, Recursion
Difficulty: Easy
Problem:
Write a function that reverses a string. The input string is given as an array of characters
You must do this by modifying the input array in-place with
Example 1:
Example 2:
Constraints:
•
•
344. Reverse String
Topic: Two Pointers, String, Recursion
Difficulty: Easy
Problem:
Write a function that reverses a string. The input string is given as an array of characters
s.You must do this by modifying the input array in-place with
O(1) extra memory.Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
•
1 <= s.length <= 10^5•
s[i] is a printable ascii character.2022-04-02
680. Valid Palindrome II
Topic: Two Pointers, String, Greedy
Difficulty: Easy
Problem:
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
680. Valid Palindrome II
Topic: Two Pointers, String, Greedy
Difficulty: Easy
Problem:
Given a string
s, return true if the s can be palindrome after deleting at most one character from it.Example 1:
Input: s = "aba"
Output: true
Example 2:
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
Example 3:
Input: s = "abc"
Output: false
Constraints:
•
1 <= s.length <= 10^5•
s consists of lowercase English letters.2022-04-03
31. Next Permutation
Topic: Array, Two Pointers
Difficulty: Medium
Problem:
A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
• For example, for
The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
• For example, the next permutation of
• Similarly, the next permutation of
• While the next permutation of
Given an array of integers
The replacement must be in place and use only constant extra memory.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
31. Next Permutation
Topic: Array, Two Pointers
Difficulty: Medium
Problem:
A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
• For example, for
arr = [1,2,3], the following are considered permutations of arr: [1,2,3], [1,3,2], [3,1,2], [2,3,1].The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
• For example, the next permutation of
arr = [1,2,3] is [1,3,2].• Similarly, the next permutation of
arr = [2,3,1] is [3,1,2].• While the next permutation of
arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.Given an array of integers
nums, find the next permutation of nums.The replacement must be in place and use only constant extra memory.
Example 1:
Input: nums = [1,2,3]
Output: [1,3,2]
Example 2:
Input: nums = [3,2,1]
Output: [1,2,3]
Example 3:
Input: nums = [1,1,5]
Output: [1,5,1]
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 1002022-04-04
1721. Swapping Nodes in a Linked List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
You are given the
Return the head of the linked list after swapping the values of the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/21/linked1.jpg
Example 2:
Constraints:
• The number of nodes in the list is
•
•
1721. Swapping Nodes in a Linked List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
You are given the
head of a linked list, and an integer k.Return the head of the linked list after swapping the values of the
k^th node from the beginning and the k^th node from the end (the list is 1-indexed).Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/21/linked1.jpg
Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]
Example 2:
Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
Output: [7,9,6,6,8,7,3,0,9,5]
Constraints:
• The number of nodes in the list is
n.•
1 <= k <= n <= 10^5•
0 <= Node.val <= 1002022-04-05
11. Container With Most Water
Topic: Array, Two Pointers, Greedy
Difficulty: Medium
Problem:
You are given an integer array
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg
Example 2:
Constraints:
•
•
•
11. Container With Most Water
Topic: Array, Two Pointers, Greedy
Difficulty: Medium
Problem:
You are given an integer array
height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i]).Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:
Input: height = [1,1]
Output: 1
Constraints:
•
n == height.length•
2 <= n <= 10^5•
0 <= height[i] <= 10^42022-04-06
923. 3Sum With Multiplicity
Topic: Array, Hash Table, Two Pointers, Sorting, Counting
Difficulty: Medium
Problem:
Given an integer array
As the answer can be very large, return it modulo
Example 1:
Example 2:
Constraints:
•
•
•
923. 3Sum With Multiplicity
Topic: Array, Hash Table, Two Pointers, Sorting, Counting
Difficulty: Medium
Problem:
Given an integer array
arr, and an integer target, return the number of tuples i, j, k such that i < j < k and arr[i] + arr[j] + arr[k] == target.As the answer can be very large, return it modulo
10^9 + 7.Example 1:
Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation:
Enumerating by the values (arr[i], arr[j], arr[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.
Example 2:
Input: arr = [1,1,2,2,2,2], target = 5
Output: 12
Explanation:
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.
Constraints:
•
3 <= arr.length <= 3000•
0 <= arr[i] <= 100•
0 <= target <= 3002022-04-07
1046. Last Stone Weight
Topic: Array, Heap (Priority Queue)
Difficulty: Easy
Problem:
You are given an array of integers
We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights
• If
• If
At the end of the game, there is at most one stone left.
Return the smallest possible weight of the left stone. If there are no stones left, return
Example 1:
Example 2:
Constraints:
•
•
1046. Last Stone Weight
Topic: Array, Heap (Priority Queue)
Difficulty: Easy
Problem:
You are given an array of integers
stones where stones[i] is the weight of the i^th stone.We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights
x and y with x <= y. The result of this smash is:• If
x == y, both stones are destroyed, and• If
x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.At the end of the game, there is at most one stone left.
Return the smallest possible weight of the left stone. If there are no stones left, return
0.Example 1:
Input: stones = [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
Example 2:
Input: stones = [1]
Output: 1
Constraints:
•
1 <= stones.length <= 30•
1 <= stones[i] <= 10002022-04-08
703. Kth Largest Element in a Stream
Topic: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
Difficulty: Easy
Problem:
Design a class to find the
Implement
•
•
Example 1:
Constraints:
•
•
•
•
• At most
• It is guaranteed that there will be at least
703. Kth Largest Element in a Stream
Topic: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
Difficulty: Easy
Problem:
Design a class to find the
k^th largest element in a stream. Note that it is the k^th largest element in the sorted order, not the k^th distinct element.Implement
KthLargest class:•
KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.•
int add(int val) Appends the integer val to the stream and returns the element representing the k^th largest element in the stream.Example 1:
Input
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output
[null, 4, 5, 5, 8, 8]
Explanation
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
Constraints:
•
1 <= k <= 10^4•
0 <= nums.length <= 10^4•
-10^4 <= nums[i] <= 10^4•
-10^4 <= val <= 10^4• At most
10^4 calls will be made to add.• It is guaranteed that there will be at least
k elements in the array when you search for the k^th element.2022-04-09
347. Top K Frequent Elements
Topic: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
• It is guaranteed that the answer is unique.
Follow up: Your algorithm's time complexity must be better than
347. Top K Frequent Elements
Topic: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect
Difficulty: Medium
Problem:
Given an integer array
nums and an integer k, return the k most frequent elements. You may return the answer in any order.Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Constraints:
•
1 <= nums.length <= 10^5•
k is in the range [1, the number of unique elements in the array].• It is guaranteed that the answer is unique.
Follow up: Your algorithm's time complexity must be better than
O(n log n), where n is the array's size.2022-04-10
682. Baseball Game
Topic: Array, Stack, Simulation
Difficulty: Easy
Problem:
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
At the beginning of the game, you start with an empty record. You are given a list of strings
1. An integer
2.
3.
4.
Return the sum of all the scores on the record.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• For operation
• For operations
682. Baseball Game
Topic: Array, Stack, Simulation
Difficulty: Easy
Problem:
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
At the beginning of the game, you start with an empty record. You are given a list of strings
ops, where ops[i] is the i^th operation you must apply to the record and is one of the following:1. An integer
x - Record a new score of x.2.
"+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.3.
"D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.4.
"C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.Return the sum of all the scores on the record.
Example 1:
Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.
Example 2:
Input: ops = ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
"5" - Add 5 to the record, record is now [5].
"-2" - Add -2 to the record, record is now [5, -2].
"4" - Add 4 to the record, record is now [5, -2, 4].
"C" - Invalidate and remove the previous score, record is now [5, -2].
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
Example 3:
Input: ops = ["1"]
Output: 1
Constraints:
•
1 <= ops.length <= 1000•
ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 10^4, 3 * 10^4].• For operation
"+", there will always be at least two previous scores on the record.• For operations
"C" and "D", there will always be at least one previous score on the record.2022-04-11
1260. Shift 2D Grid
Topic: Array, Matrix, Simulation
Difficulty: Easy
Problem:
Given a 2D
In one shift operation:
• Element at
• Element at
• Element at
Return the 2D grid after applying shift operation
Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/05/e1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2019/11/05/e2.png
Example 3:
Constraints:
•
•
•
•
•
•
1260. Shift 2D Grid
Topic: Array, Matrix, Simulation
Difficulty: Easy
Problem:
Given a 2D
grid of size m x n and an integer k. You need to shift the grid k times.In one shift operation:
• Element at
grid[i][j] moves to grid[i][j + 1].• Element at
grid[i][n - 1] moves to grid[i + 1][0].• Element at
grid[m - 1][n - 1] moves to grid[0][0].Return the 2D grid after applying shift operation
k times.Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/05/e1.png
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]
Example 2:
Image: https://assets.leetcode.com/uploads/2019/11/05/e2.png
Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
Example 3:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m <= 50•
1 <= n <= 50•
-1000 <= grid[i][j] <= 1000•
0 <= k <= 1002022-04-12
289. Game of Life
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
The board is made up of an
1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by over-population.
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg
Constraints:
•
•
•
•
Follow up:
• Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
• In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
289. Game of Life
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
The board is made up of an
m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by over-population.
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the
m x n grid board, return the next state.Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg
Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]
Constraints:
•
m == board.length•
n == board[i].length•
1 <= m, n <= 25•
board[i][j] is 0 or 1.Follow up:
• Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
• In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
2022-04-13
59. Spiral Matrix II
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
Given a positive integer
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg
Example 2:
Constraints:
•
59. Spiral Matrix II
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
Given a positive integer
n, generate an n x n matrix filled with elements from 1 to n^2 in spiral order.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]
Example 2:
Input: n = 1
Output: [[1]]
Constraints:
•
1 <= n <= 202022-04-14
700. Search in a Binary Search Tree
Topic: Tree, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
You are given the
Find the node in the BST that the node's value equals
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg
Constraints:
• The number of nodes in the tree is in the range
•
•
•
700. Search in a Binary Search Tree
Topic: Tree, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
You are given the
root of a binary search tree (BST) and an integer val.Find the node in the BST that the node's value equals
val and return the subtree rooted with that node. If such a node does not exist, return null.Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg
Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]
Example 2:
Image: https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg
Input: root = [4,2,7,1,3], val = 5
Output: []
Constraints:
• The number of nodes in the tree is in the range
[1, 5000].•
1 <= Node.val <= 10^7•
root is a binary search tree.•
1 <= val <= 10^72022-04-15
669. Trim a Binary Search Tree
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given the
Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg
Constraints:
• The number of nodes in the tree in the range
•
• The value of each node in the tree is unique.
•
•
669. Trim a Binary Search Tree
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a unique answer.Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg
Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg
Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output: [3,2,null,1]
Constraints:
• The number of nodes in the tree in the range
[1, 10^4].•
0 <= Node.val <= 10^4• The value of each node in the tree is unique.
•
root is guaranteed to be a valid binary search tree.•
0 <= low <= high <= 10^42022-04-16
538. Convert BST to Greater Tree
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given the
As a reminder, a binary search tree is a tree that satisfies these constraints:
• The left subtree of a node contains only nodes with keys less than the node's key.
• The right subtree of a node contains only nodes with keys greater than the node's key.
• Both the left and right subtrees must also be binary search trees.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/05/02/tree.png
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
• All the values in the tree are unique.
•
Note: This question is the same as 1038: <https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/>
538. Convert BST to Greater Tree
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.As a reminder, a binary search tree is a tree that satisfies these constraints:
• The left subtree of a node contains only nodes with keys less than the node's key.
• The right subtree of a node contains only nodes with keys greater than the node's key.
• Both the left and right subtrees must also be binary search trees.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/05/02/tree.png
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Example 2:
Input: root = [0,null,1]
Output: [1,null,1]
Constraints:
• The number of nodes in the tree is in the range
[0, 10^4].•
-10^4 <= Node.val <= 10^4• All the values in the tree are unique.
•
root is guaranteed to be a valid binary search tree.Note: This question is the same as 1038: <https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/>
2022-04-17
897. Increasing Order Search Tree
Topic: Stack, Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/17/ex1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/17/ex2.jpg
Constraints:
• The number of nodes in the given tree will be in the range
•
897. Increasing Order Search Tree
Topic: Stack, Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/17/ex1.jpg
Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/17/ex2.jpg
Input: root = [5,1,7]
Output: [1,null,5,null,7]
Constraints:
• The number of nodes in the given tree will be in the range
[1, 100].•
0 <= Node.val <= 1000