Skip to content

Commit 439c23b

Browse files
add three solution for bst
1 parent e7a9abd commit 439c23b

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
|714|[Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee) | |Medium|
6565
|712|[Minimum ASCII Sum for Two Strings](https://leetcode.com/problems/minimum-ascii--sum-for-two-strings/) | |Medium|
6666
|704|[Binary Search](https://leetcode.com/problems/binary-search/)| [Java](./algorithms/binarySearch/Solution.java)
67+
|701|[Insert Into A Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/)| [Java](./algorithms/insertIntoABST/Solution.java)
68+
|700|[Search In A Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/)| [Java](./algorithms/searchInABST/Solution.java)
6769
|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | |Medium|
6870
|687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/) | |Easy|
6971
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/) | |Medium|
@@ -94,6 +96,7 @@
9496
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | |Easy|
9597
|518|[Coin Change 2](https://leetcode.com/problems/coin-change-2/) | |Medium|
9698
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [java](./algorithms/fibonacciNumber/Solution.java) |Easy|
99+
|501|[Find Mode In Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/) | [java](./algorithms/findModeInBST/Solution.java) |Easy|
97100
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | |Medium|
98101
|463|[Island Perimeter](https://leetcode.com/problems/island-perimeter/) | |Easy|
99102
|450|[NodeInABST](https://leetcode.com/problems/-node-in-a-bst/) | |Medium|
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
List<Integer> answer = new ArrayList<Integer>();
12+
int base, count, maxCount;
13+
14+
public int[] findMode(TreeNode root) {
15+
dfs(root);
16+
int[] mode = new int[answer.size()];
17+
for (int i = 0; i < answer.size(); ++i) {
18+
mode[i] = answer.get(i);
19+
}
20+
return mode;
21+
}
22+
23+
public void dfs(TreeNode o) {
24+
if (o == null) {
25+
return;
26+
}
27+
dfs(o.left);
28+
update(o.val);
29+
dfs(o.right);
30+
}
31+
32+
public void update(int x) {
33+
if (x == base) {
34+
++count;
35+
} else {
36+
count = 1;
37+
base = x;
38+
}
39+
if (count == maxCount) {
40+
answer.add(base);
41+
}
42+
if (count > maxCount) {
43+
maxCount = count;
44+
answer.clear();
45+
answer.add(base);
46+
}
47+
}
48+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode insertIntoBST(TreeNode root, int val) {
18+
if (root == null) {
19+
return new TreeNode(val);
20+
}
21+
//指针
22+
TreeNode pos = root;
23+
//迭代
24+
while (pos != null) {
25+
if (val < pos.val) {
26+
if (pos.left == null) {
27+
pos.left = new TreeNode(val);
28+
break;
29+
} else {
30+
pos = pos.left;
31+
}
32+
} else {
33+
if (pos.right == null) {
34+
pos.right = new TreeNode(val);
35+
break;
36+
} else {
37+
pos = pos.right;
38+
}
39+
}
40+
}
41+
return root;
42+
}
43+
}

algorithms/searchInABST/Solution.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode searchBST(TreeNode root, int val) {
18+
if(root == null || root.val == val){
19+
return root;
20+
}
21+
return root.val > val ? searchBST(root.left,val):searchBST(root.right,val);
22+
}
23+
}

0 commit comments

Comments
 (0)