File tree 4 files changed +117
-0
lines changed
4 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 64
64
| 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|
65
65
| 712| [ Minimum ASCII Sum for Two Strings] ( https://leetcode.com/problems/minimum-ascii--sum-for-two-strings/ ) | | Medium|
66
66
|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 )
67
69
| 695| [ Max Area of Island] ( https://leetcode.com/problems/max-area-of-island/ ) | | Medium|
68
70
| 687| [ Longest Univalue Path] ( https://leetcode.com/problems/longest-univalue-path/ ) | | Easy|
69
71
| 684| [ Redundant Connection] ( https://leetcode.com/problems/redundant-connection/ ) | | Medium|
94
96
| 520| [ Detect Capital] ( https://leetcode.com/problems/detect-capital/ ) | | Easy|
95
97
| 518| [ Coin Change 2] ( https://leetcode.com/problems/coin-change-2/ ) | | Medium|
96
98
| 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|
97
100
| 477| [ Total Hamming Distance] ( https://leetcode.com/problems/total-hamming-distance/ ) | | Medium|
98
101
| 463| [ Island Perimeter] ( https://leetcode.com/problems/island-perimeter/ ) | | Easy|
99
102
| 450| [ NodeInABST] ( https://leetcode.com/problems/-node-in-a-bst/ ) | | Medium|
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments