-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathFindModeInBinarySearchTree501.java
135 lines (121 loc) · 3.57 KB
/
FindModeInBinarySearchTree501.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Given a binary search tree (BST) with duplicates, find all the mode(s)
* (the most frequently occurred element) in the given BST.
*
* Assume a BST is defined as follows:
* - The left subtree of a node contains only nodes with keys less than or equal to the node's key.
* - The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
* - Both the left and right subtrees must also be binary search trees.
* For example:
* Given BST [1,null,2,2],
*
* 1
* \
* 2
* /
* 2
*
* return [2].
*
* Note: If a tree has more than one mode, you can return them in any order.
*
* Follow up: Could you do that without using any extra space? (Assume that the
* implicit stack space incurred due to recursion does not count).
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class FindModeInBinarySearchTree501 {
public int[] findMode(TreeNode root) {
Result result = new Result();
findMode(root, result);
int[] output = new int[result.res.size()];
int i = 0;
for (int r: result.res) {
output[i++] = r;
}
return output;
}
private void findMode(TreeNode root, Result result) {
if (root == null) return;
findMode(root.left, result);
if (result.maxCount == -1) {
result.maxCount = 1;
result.currCount = 1;
result.curr = root.val;
result.res = new HashSet<>();
result.res.add(root.val);
} else if (result.curr != root.val) {
result.currCount = 1;
result.curr = root.val;
if (result.maxCount == result.currCount) {
result.res.add(root.val);
}
} else {
result.currCount++;
if (result.maxCount == result.currCount) {
result.res.add(root.val);
} else if (result.maxCount < result.currCount) {
result.maxCount = result.currCount;
result.res = new HashSet<>();
result.res.add(root.val);
}
}
findMode(root.right, result);
}
class Result {
int maxCount;
int currCount;
int curr;
Set<Integer> res;
Result() {
this.maxCount = -1;
this.currCount = -1;
this.curr = 0;
this.res = new HashSet<>();
}
}
/**
* https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/98101/Proper-O(1)-space
*/
public int[] findMode2(TreeNode root) {
inorder(root);
modes = new int[modeCount];
modeCount = 0;
currCount = 0;
inorder(root);
return modes;
}
private int currVal;
private int currCount = 0;
private int maxCount = 0;
private int modeCount = 0;
private int[] modes;
private void handleValue(int val) {
if (val != currVal) {
currVal = val;
currCount = 0;
}
currCount++;
if (currCount > maxCount) {
maxCount = currCount;
modeCount = 1;
} else if (currCount == maxCount) {
if (modes != null)
modes[modeCount] = currVal;
modeCount++;
}
}
private void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
handleValue(root.val);
inorder(root.right);
}
}