Skip to content

Commit 9b3ed41

Browse files
committed
[Function add]
1. Add leetcode solutions with tag BST.
1 parent 59f2785 commit 9b3ed41

10 files changed

+557
-2
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,28 @@
697697
* [337. House Robber III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/337.%20House%20Robber%20III.md)
698698
* [979. Distribute Coins in Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/979.%20Distribute%20Coins%20in%20Binary%20Tree.md)
699699

700+
### Binary Search
701+
* [34. Find First and Last Position of Element in Sorted Array](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/34.%20Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array.md)
702+
* [35. Search Insert Position](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/35.%20Search%20Insert%20Position.md)
703+
* [704. Binary Search](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/704.%20Binary%20Search.md)
704+
* [981. Time Based Key-Value Store](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/981.%20Time%20Based%20Key-Value%20Store.md)
705+
* [33. Search in Rotated Sorted Array](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/33.%20Search%20in%20Rotated%20Sorted%20Array.md)
706+
* [81. Search in Rotated Sorted Array II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/81.%20Search%20in%20Rotated%20Sorted%20Array%20II.md)
707+
* [153. Find Minimum in Rotated Sorted Array](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/153.%20Find%20Minimum%20in%20Rotated%20Sorted%20Array.md)
708+
* [154. Find Minimum in Rotated Sorted Array II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/154.%20Find%20Minimum%20in%20Rotated%20Sorted%20Array%20II.md)
709+
* [162. Find Peak Element](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/162.%20Find%20Peak%20Element.md)
710+
* [852. Peak Index in a Mountain Array](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/852.%20Peak%20Index%20in%20a%20Mountain%20Array.md)
711+
* [69. Sqrt(x)](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/69.%20Sqrt(x).md)
712+
* [74. Search a 2D Matrix](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/74.%20Search%20a%202D%20Matrix.md)
713+
* [378. Kth Smallest Element in a Sorted Matrix](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/378.%20Kth%20Smallest%20Element%20in%20a%20Sorted%20Matrix.md)
714+
* [668. Kth Smallest Number in Multiplication Table](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/668.%20Kth%20Smallest%20Number%20in%20Multiplication%20Table.md)
715+
* [778. Swim in Rising Water](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/778.%20Swim%20in%20Rising%20Water.md)
716+
* [174. Dungeon Game](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/174.%20Dungeon%20Game.md)
717+
* [875. Koko Eating Bananas](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/875.%20Koko%20Eating%20Bananas.md)
718+
* [4.Median of Two Sorted Arrays](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/4.%20Median%20of%20Two%20Sorted%20Arrays.md)
719+
* [719. Find K-th Smallest Pair Distance](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/719.%20Find%20K-th%20Smallest%20Pair%20Distance.md)
720+
* [786. K-th Smallest Prime Fraction](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/786.%20K-th%20Smallest%20Prime%20Fraction.md)
721+
700722
## Algorithm(4th_Edition)
701723
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
702724
All java realization codes are placed in different packages.

leetcode/108. Convert Sorted Array to Binary Search Tree.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,33 @@ class Solution {
7777
return node;
7878
}
7979
}
80-
```
80+
```
81+
82+
### Third Time
83+
* Method 1: BST + binary search
84+
```Java
85+
/**
86+
* Definition for a binary tree node.
87+
* public class TreeNode {
88+
* int val;
89+
* TreeNode left;
90+
* TreeNode right;
91+
* TreeNode(int x) { val = x; }
92+
* }
93+
*/
94+
class Solution {
95+
private int[] nums;
96+
public TreeNode sortedArrayToBST(int[] nums) {
97+
this.nums = nums;
98+
return createTree(0, nums.length - 1);
99+
}
100+
private TreeNode createTree(int low, int high){
101+
if(low > high) return null;
102+
int mid = low + (high - low) / 2;
103+
TreeNode node = new TreeNode(nums[mid]);
104+
node.left = createTree(low, mid - 1);
105+
node.right = createTree(mid + 1, high);
106+
return node;
107+
}
108+
}
109+
```

leetcode/230. Kth Smallest Element in a BST.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,31 @@ class Solution {
9090
}
9191
}
9292
```
93+
94+
### Third Time
95+
* Method 1: BST
96+
```Java
97+
/**
98+
* Definition for a binary tree node.
99+
* public class TreeNode {
100+
* int val;
101+
* TreeNode left;
102+
* TreeNode right;
103+
* TreeNode(int x) { val = x; }
104+
* }
105+
*/
106+
class Solution {
107+
private List<Integer> res;
108+
public int kthSmallest(TreeNode root, int k) {
109+
this.res = new ArrayList<>();
110+
dfs(root, k);
111+
return this.res.get(k - 1);
112+
}
113+
private void dfs(TreeNode node, int k){
114+
if(node == null || res.size() > k) return;
115+
dfs(node.left, k);
116+
this.res.add(node.val);
117+
dfs(node.right, k);
118+
}
119+
}
120+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## 501. Find Mode in Binary Search Tree
2+
3+
### Question
4+
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.
5+
6+
Assume a BST is defined as follows:
7+
* The left subtree of a node contains only nodes with keys less than or equal to the node's key.
8+
* The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
9+
* Both the left and right subtrees must also be binary search trees.
10+
11+
12+
```
13+
For example:
14+
Given BST [1,null,2,2],
15+
16+
1
17+
\
18+
2
19+
/
20+
2
21+
22+
23+
24+
return [2].
25+
```
26+
27+
Note: If a tree has more than one mode, you can return them in any order.
28+
29+
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
30+
31+
### Solution
32+
* Method 1: HashMap + dfs
33+
```Java
34+
/**
35+
* Definition for a binary tree node.
36+
* public class TreeNode {
37+
* int val;
38+
* TreeNode left;
39+
* TreeNode right;
40+
* TreeNode(int x) { val = x; }
41+
* }
42+
*/
43+
class Solution {
44+
private Map<Integer, Integer> map;
45+
private int max = Integer.MIN_VALUE;
46+
public int[] findMode(TreeNode root) {
47+
if(root == null) return new int[]{};
48+
this.map = new HashMap<>();
49+
dfs(root);
50+
int count = 0;
51+
for(int i : map.values()){
52+
if(i == max) count++;
53+
}
54+
int[] res = new int[count];
55+
int i = 0;
56+
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
57+
if(entry.getValue() == max)
58+
res[i++] = entry.getKey();
59+
}
60+
return res;
61+
}
62+
private void dfs(TreeNode node){
63+
if(node == null) return;
64+
Integer count = map.containsKey(node.val) ? map.get(node.val): 0;
65+
map.put(node.val, count + 1);
66+
this.max = Math.max(max, count + 1);
67+
dfs(node.left);
68+
dfs(node.right);
69+
}
70+
}
71+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## 530. Minimum Absolute Difference in BST
2+
3+
### Question:
4+
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
5+
6+
```
7+
Example:
8+
9+
Input:
10+
11+
1
12+
\
13+
3
14+
/
15+
2
16+
17+
Output:
18+
1
19+
20+
Explanation:
21+
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
22+
```
23+
24+
Note: There are at least two nodes in this BST.
25+
26+
27+
### Solution:
28+
* Method 1: BST
29+
```Java
30+
/**
31+
* Definition for a binary tree node.
32+
* public class TreeNode {
33+
* int val;
34+
* TreeNode left;
35+
* TreeNode right;
36+
* TreeNode(int x) { val = x; }
37+
* }
38+
*/
39+
class Solution {
40+
private int res = Integer.MAX_VALUE;
41+
public int getMinimumDifference(TreeNode root) {
42+
int[] range = getRange(root);
43+
return this.res;
44+
}
45+
private int[] getRange(TreeNode node){
46+
int cur = node.val;
47+
int[] left = null, right = null;
48+
if(node.left != null){
49+
left = getRange(node.left);
50+
this.res = Math.min(res, cur - left[1]);
51+
}
52+
if(node.right != null){
53+
right = getRange(node.right);
54+
this.res = Math.min(res, right[0] - cur);
55+
}
56+
if(left == null && right == null) return new int[]{cur, cur};
57+
else if(left != null && right != null) return new int[]{left[0], right[1]};
58+
else{
59+
return left == null ? new int[]{cur, right[1]}: new int[]{left[0], cur};
60+
}
61+
}
62+
}
63+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## 700. Search in a Binary Search Tree
2+
3+
### Question:
4+
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.
5+
6+
```
7+
For example,
8+
9+
Given the tree:
10+
4
11+
/ \
12+
2 7
13+
/ \
14+
1 3
15+
16+
And the value to search: 2
17+
18+
You should return this subtree:
19+
20+
2
21+
/ \
22+
1 3
23+
24+
In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.
25+
```
26+
27+
Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.
28+
29+
### Solution:
30+
* Method 1: BST
31+
```Java
32+
/**
33+
* Definition for a binary tree node.
34+
* public class TreeNode {
35+
* int val;
36+
* TreeNode left;
37+
* TreeNode right;
38+
* TreeNode(int x) { val = x; }
39+
* }
40+
*/
41+
class Solution {
42+
public TreeNode searchBST(TreeNode root, int val) {
43+
if(root == null) return null;
44+
if(root.val == val) return root;
45+
return searchBST(val < root.val ? root.left: root.right, val);
46+
}
47+
}
48+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 701. Insert into a Binary Search Tree
2+
3+
### Question
4+
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
5+
6+
Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
7+
8+
```
9+
For example,
10+
11+
Given the tree:
12+
4
13+
/ \
14+
2 7
15+
/ \
16+
1 3
17+
And the value to insert: 5
18+
19+
You can return this binary search tree:
20+
21+
4
22+
/ \
23+
2 7
24+
/ \ /
25+
1 3 5
26+
27+
This tree is also valid:
28+
29+
5
30+
/ \
31+
2 7
32+
/ \
33+
1 3
34+
\
35+
4
36+
```
37+
38+
### Solution
39+
* Method 1: BST, recursion
40+
```Java
41+
/**
42+
* Definition for a binary tree node.
43+
* public class TreeNode {
44+
* int val;
45+
* TreeNode left;
46+
* TreeNode right;
47+
* TreeNode(int x) { val = x; }
48+
* }
49+
*/
50+
class Solution {
51+
public TreeNode insertIntoBST(TreeNode root, int val) {
52+
if(root == null) return new TreeNode(val);
53+
if(val < root.val){
54+
root.left = insertIntoBST(root.left, val);
55+
}else root.right = insertIntoBST(root.right, val);
56+
return root;
57+
}
58+
}
59+
```

0 commit comments

Comments
 (0)