Leetcode#114. Flatten Binary Tree to Linked List
ProblemGiven the root of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The “linked list” should be in the same order as a pre-order traversal of the binary tree.
Example 1:
!https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg
123Input: root = [1,2,5,3,4,null,6]Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
1 ...
Leetcode#450. Delete Node in a BST
ProblemGiven a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
Search for a node to remove.
If the node is found, delete the node.
Example 1:
!https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg
123456Input: root = [5,3,6,2,4,null,7], key = 3Output: [5,4,6,2,null,null,7]Explanation: Given key to delete is 3. So we find the node ...
Leetcode#101. Symmetric Tree
ProblemGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg
123Input: root = [1,2,2,3,4,4,3]Output: true
Example 2:
!https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg
123Input: root = [1,2,2,null,3,null,3]Output: false
Constraints:
The number of nodes in the tree is in the range [1, 1000].
100 <= Node.val <= 100
Follow up:
Could you solve it both r ...
Leetcode#322. Coin Change
ProblemYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
1234Input: coins = [1,2,5], amount = 11Output: 3Explanation: 11 = 5 + 5 + 1
Example 2:
123Input: coins = [2], ...
Leetcode#104. Maximum Depth of Binary Tree
ProblemGiven the root of a binary tree, return its maximum depth.
A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
!https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg
123Input: root = [3,9,20,null,null,15,7]Output: 3
Example 2:
123Input: root = [1,null,2]Output: 2
Constraints:
The number of nodes in the tree is in the range [0, 104].
100 <= Node.val <= 100
Solve解11234567891011121314151617 ...
Leetcode#380. Insert Delete GetRandom O(1)
ProblemImplement the RandomizedSet class:
RandomizedSet() Initializes the RandomizedSet object.
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
int getRandom() Returns a random element from the current set of elements (it’s guaranteed that at least one element exists when this method is called). ...
Leetcode#274. H-Index
ProblemGiven an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher’s h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
Example 1:
12345Input: citations = [3,0,6,1,5]Output: 3Explanation: [3,0,6,1,5] means the researcher has 5 papers in total an ...
Leetcode#122. Best Time to Buy and Sell Stock II
ProblemYou are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
123456Input: prices = [7,1,5,3,6,4]Output: 7Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.Then buy ...
Leetcode#189. Rotate Arra
ProblemGiven an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
1234567Input: nums = [1,2,3,4,5,6,7], k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5,6]rotate 2 steps to the right: [6,7,1,2,3,4,5]rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
123456Input: nums = [-1,-100,3,99], k = 2Output: [3,99,-1,-100]Explanation:rotate 1 steps to the right: [99,-1,-100,3]rotate 2 steps to the right: [3,99,-1,-100]
...
Leetcode#169. Majority Element
ProblemGiven an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Example 1:
123Input: nums = [3,2,3]Output: 3
Example 2:
123Input: nums = [2,2,1,1,1,2,2]Output: 2
Constraints:
n == nums.length
1 <= n <= 5 * 104
109 <= nums[i] <= 109
Follow-up:
Could you solve the problem in linear time and in
1O(1)
space?
Solve解112345678910class Solutio ...