5
5
import com .inuker .solution .InorderSuccessorInBST ;
6
6
import com .leetcode .library .TreeNode ;
7
7
8
+ import java .util .ArrayList ;
8
9
import java .util .LinkedList ;
9
10
import java .util .List ;
10
11
import java .util .Stack ;
@@ -22,56 +23,62 @@ public static void main(String[] args) {
22
23
}
23
24
}
24
25
25
- public static List <Integer > closestKValues (TreeNode root , double target , int k ) {
26
- Stack <TreeNode > preStack = new Stack <>();
27
- Stack <TreeNode > postStack = new Stack <>();
28
-
29
- for (TreeNode node = root ; node != null ; ) {
30
- if (target <= node .val ) {
31
- postStack .push (node );
32
- node = node .left ;
33
- } else {
34
- preStack .push (node );
35
- node = node .right ;
36
- }
37
- }
26
+ int mMaxCount ;
27
+ int mCurVal ;
28
+ int mCurCount ;
29
+ int mMaxElemSize ;
38
30
39
- List <Integer > list = new LinkedList <>();
40
-
41
- for (int i = 0 ; i < k ; i ++) {
42
- if (preStack .isEmpty ()) {
43
- list .add (getNextSuccessor (postStack ).val );
44
- } else if (postStack .isEmpty ()) {
45
- list .add (getNextPredesessor (preStack ).val );
46
- } else if (Math .abs (target - preStack .peek ().val ) < Math .abs (target - postStack .peek ().val )) {
47
- list .add (getNextPredesessor (preStack ).val );
48
- } else {
49
- list .add (getNextSuccessor (postStack ).val );
50
- }
51
- }
31
+ int idx ;
52
32
53
- return list ;
33
+ public int [] findMode (TreeNode root ) {
34
+ helper (root );
35
+ int [] result = new int [mMaxElemSize ];
36
+ mCurCount = 0 ;
37
+ unite (root , result );
38
+ return result ;
54
39
}
55
40
56
- private static TreeNode getNextPredesessor (Stack <TreeNode > stack ) {
57
- if (stack .isEmpty ()) {
58
- return null ;
41
+ private void unite (TreeNode node , int [] result ) {
42
+ if (node == null ) {
43
+ return ;
44
+ }
45
+ unite (node .left , result );
46
+
47
+ if (node .val == mCurVal ) {
48
+ mCurCount ++;
49
+ } else {
50
+ mCurCount = 1 ;
51
+ mCurVal = node .val ;
59
52
}
60
- TreeNode ret = stack . pop ();
61
- for ( TreeNode node = ret . left ; node != null ; node = node . right ) {
62
- stack . push ( node ) ;
53
+
54
+ if ( mCurCount == mMaxCount ) {
55
+ result [ idx ++] = mCurVal ;
63
56
}
64
- return ret ;
57
+
58
+ unite (node .right , result );
65
59
}
66
60
67
- private static TreeNode getNextSuccessor ( Stack < TreeNode > stack ) {
68
- if (stack . isEmpty () ) {
69
- return null ;
61
+ private void helper ( TreeNode node ) {
62
+ if (node == null ) {
63
+ return ;
70
64
}
71
- TreeNode ret = stack .pop ();
72
- for (TreeNode node = ret .right ; node != null ; node = node .left ) {
73
- stack .push (node );
65
+
66
+ helper (node .left );
67
+
68
+ if (node .val == mCurVal ) {
69
+ mCurCount ++;
70
+ } else {
71
+ mCurCount = 1 ;
72
+ mCurVal = node .val ;
74
73
}
75
- return ret ;
74
+
75
+ if (mCurCount == mMaxCount ) {
76
+ mMaxElemSize ++;
77
+ } else if (mCurCount > mMaxCount ) {
78
+ mMaxElemSize = 1 ;
79
+ mMaxCount = mCurCount ;
80
+ }
81
+
82
+ helper (node .right );
76
83
}
77
84
}
0 commit comments