File tree 3 files changed +102
-5
lines changed
3 files changed +102
-5
lines changed Original file line number Diff line number Diff line change 1
1
# LeetCode Algorithms
2
2
3
- ![ problems-solved] ( https://img.shields.io/badge/Problems%20Solved-108 /571-1f425f.svg )
4
- ![ problems-solved-java] ( https://img.shields.io/badge/Java-108 /1571-1abc9c.svg )
5
- ![ problems-solved-python] ( https://img.shields.io/badge/Python-108 /1571-1abc9c.svg )
3
+ ![ problems-solved] ( https://img.shields.io/badge/Problems%20Solved-109 /571-1f425f.svg )
4
+ ![ problems-solved-java] ( https://img.shields.io/badge/Java-109 /1571-1abc9c.svg )
5
+ ![ problems-solved-python] ( https://img.shields.io/badge/Python-109 /1571-1abc9c.svg )
6
6
[ ![ PRs Welcome] ( https://img.shields.io/badge/PRs-welcome-brightgreen.svg )] ( CONTRIBUTING.md )
7
7
8
8
🔒 = Subscription Content
132
132
| 492 | [ Construct the Rectangle] ( https://leetcode.com/problems/construct-the-rectangle ) | Easy | [ ![ Java] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( src/ConstructTheRectangle.java ) [ ![ Python] ( https://img.icons8.com/color/35/000000/python.png )] ( python/construct_the_rectangle.py ) |
133
133
| 496 | [ Next Greater Element I] ( https://leetcode.com/problems/next-greater-element-i ) | Easy | [ ![ Java] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( src/NextGreaterElementI.java ) [ ![ Python] ( https://img.icons8.com/color/35/000000/python.png )] ( python/next_greater_element_i.py ) |
134
134
| 500 | [ Keyboard Row] ( https://leetcode.com/problems/keyboard-row ) | Easy | [ ![ Java] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( src/KeyBoardRow.java ) [ ![ Python] ( https://img.icons8.com/color/35/000000/python.png )] ( python/keyboard_row.py ) |
135
- | 501 | [ Find Mode in Binary Search Tree] ( https://leetcode.com/problems/find-mode-in-binary-search-tree ) | Easy | |
135
+ | 501 | [ Find Mode in Binary Search Tree] ( https://leetcode.com/problems/find-mode-in-binary-search-tree ) | Easy | [ ![ Java ] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( src/FindModeInBinarySearchTree ) [ ![ Python ] ( https://img.icons8.com/color/35/000000/python.png )] ( python/find_mode_in_binary_search_tree.py ) |
136
136
| 504 | [ Base 7] ( https://leetcode.com/problems/base-7 ) | Easy | |
137
137
| 506 | [ Relative Ranks] ( https://leetcode.com/problems/relative-ranks ) | Easy | |
138
138
| 507 | [ Perfect Number] ( https://leetcode.com/problems/perfect-number ) | Easy | |
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ from typing import List
3
+
4
+
5
+ class TreeNode :
6
+ def __init__ (self , val = 0 , left = None , right = None ):
7
+ self .val = val
8
+ self .left = left
9
+ self .right = right
10
+
11
+
12
+ class Solution :
13
+ def __init__ (self ):
14
+ self .count = 1
15
+ self .previous = None
16
+ self .max = 0
17
+
18
+ def find_mode (self , root : TreeNode , modes : List [int ]):
19
+ if root is None :
20
+ return
21
+ self .find_mode (root .left , modes )
22
+ if self .previous :
23
+ self .count = self .count + 1 if root .val == self .previous .val else 1
24
+ if self .count > self .max :
25
+ self .max = self .count
26
+ modes .clear ()
27
+ modes .append (root .val )
28
+ elif self .count == self .max :
29
+ modes .append (root .val )
30
+ self .previous = root
31
+ self .find_mode (root .right , modes )
32
+
33
+ def findMode (self , root : TreeNode ) -> List [int ]:
34
+ if root is None : return []
35
+ result = []
36
+ self .find_mode (root , result )
37
+ return result
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .Arrays ;
3
+ import java .util .List ;
4
+
1
5
public class FindModeInBinarySearchTree {
2
-
6
+ static class TreeNode {
7
+ int val ;
8
+ TreeNode left ;
9
+ TreeNode right ;
10
+ TreeNode () {}
11
+ TreeNode (int val ) { this .val = val ; }
12
+ TreeNode (int val , TreeNode left , TreeNode right ) {
13
+ this .val = val ;
14
+ this .left = left ;
15
+ this .right = right ;
16
+ }
17
+ }
18
+
19
+ private static TreeNode previous ;
20
+ private static int count = 1 ;
21
+ private static int mode = 0 ;
22
+
23
+ public static int [] findMode (TreeNode root ) {
24
+ if (root == null ) {
25
+ return new int [0 ];
26
+ }
27
+
28
+ List <Integer > modes = new ArrayList <>();
29
+ findMode (root , modes );
30
+ int [] result = new int [modes .size ()];
31
+ for (int index = 0 ; index < modes .size () ; index ++) {
32
+ result [index ] = modes .get (index );
33
+ }
34
+ previous = null ;
35
+ count = 1 ;
36
+ mode = 0 ;
37
+ return result ;
38
+ }
39
+
40
+ private static void findMode (TreeNode root , List <Integer > modes ) {
41
+ if (root == null ) {
42
+ return ;
43
+ }
44
+ findMode (root .left , modes );
45
+ if (previous != null ) {
46
+ count = root .val == previous .val ? count + 1 : 1 ;
47
+ }
48
+ if (count > mode ) {
49
+ mode = count ;
50
+ modes .clear ();
51
+ modes .add (root .val );
52
+ } else if (count == mode ) {
53
+ modes .add (root .val );
54
+ }
55
+ previous = root ;
56
+ findMode (root .right , modes );
57
+ }
58
+
59
+ public static void main (String [] args ) {
60
+ TreeNode root = new TreeNode (0 );
61
+ System .out .println (Arrays .toString (findMode (root )));
62
+ }
3
63
}
You can’t perform that action at this time.
0 commit comments