Skip to content

Commit 346e1ca

Browse files
[N-0] refactor 501
1 parent 7ae30b6 commit 346e1ca

File tree

2 files changed

+77
-40
lines changed

2 files changed

+77
-40
lines changed

src/main/java/com/fishercoder/solutions/_501.java

+70-36
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212

1313
/**
14-
Given a binary search tree with duplicates. You have to find all the mode(s) in given binary tree.
14+
* 501. Find Mode in Binary Search Tree
15+
* Given a binary search tree with duplicates. You have to find all the mode(s) in given binary tree.
1516
1617
For example:
1718
Given binary tree [1,null,2,2],
18-
1
19-
\
19+
1
20+
\
2021
2
2122
/
2223
2
@@ -27,50 +28,83 @@ Given a binary search tree with duplicates. You have to find all the mode(s) in
2728
*/
2829
public class _501 {
2930

30-
public int[] findMode(TreeNode root) {
31-
int[] result = new int[]{};
32-
Map<Integer, Integer> map = new HashMap();
33-
if (root == null) {
31+
public static class Solution1 {
32+
public int[] findMode(TreeNode root) {
33+
int[] result = new int[]{};
34+
Map<Integer, Integer> map = new HashMap();
35+
if (root == null) {
36+
return result;
37+
}
38+
List<Integer> list = bfs(root, map);
39+
result = new int[list.size()];
40+
for (int i = 0; i < list.size(); i++) {
41+
result[i] = list.get(i);
42+
}
3443
return result;
3544
}
36-
List<Integer> list = bfs(root, map);
37-
result = new int[list.size()];
38-
for (int i = 0; i < list.size(); i++) {
39-
result[i] = list.get(i);
40-
}
41-
return result;
42-
}
4345

44-
private List<Integer> bfs(TreeNode root, Map<Integer, Integer> map) {
45-
Queue<TreeNode> queue = new LinkedList<>();
46-
queue.offer(root);
47-
while (!queue.isEmpty()) {
48-
int size = queue.size();
49-
for (int i = 0; i < size; i++) {
50-
TreeNode treeNode = queue.poll();
51-
if (treeNode.left != null) {
52-
queue.offer(treeNode.left);
46+
private List<Integer> bfs(TreeNode root, Map<Integer, Integer> map) {
47+
Queue<TreeNode> queue = new LinkedList<>();
48+
queue.offer(root);
49+
while (!queue.isEmpty()) {
50+
int size = queue.size();
51+
for (int i = 0; i < size; i++) {
52+
TreeNode treeNode = queue.poll();
53+
if (treeNode.left != null) {
54+
queue.offer(treeNode.left);
55+
}
56+
if (treeNode.right != null) {
57+
queue.offer(treeNode.right);
58+
}
59+
map.put(treeNode.val, map.getOrDefault(treeNode.val, 0) + 1);
5360
}
54-
if (treeNode.right != null) {
55-
queue.offer(treeNode.right);
61+
}
62+
63+
int highestFrequency = 0;
64+
List<Integer> list = new ArrayList<>();
65+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
66+
if (entry.getValue() > highestFrequency) {
67+
highestFrequency = entry.getValue();
68+
list.clear();
69+
list.add(entry.getKey());
70+
} else if (entry.getValue() == highestFrequency) {
71+
list.add(entry.getKey());
5672
}
57-
map.put(treeNode.val, map.getOrDefault(treeNode.val, 0) + 1);
5873
}
74+
75+
return list;
5976
}
77+
}
6078

61-
int highestFrequency = 0;
62-
List<Integer> list = new ArrayList<>();
63-
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
64-
if (entry.getValue() > highestFrequency) {
65-
highestFrequency = entry.getValue();
66-
list.clear();
67-
list.add(entry.getKey());
68-
} else if (entry.getValue() == highestFrequency) {
69-
list.add(entry.getKey());
79+
public static class Solution2 {
80+
public int[] findMode(TreeNode root) {
81+
Map<Integer, Integer> map = new HashMap<>();
82+
dfs(root, map);
83+
int modeCount = 0;
84+
for (int key : map.keySet()) {
85+
modeCount = Math.max(modeCount, map.get(key));
86+
}
87+
List<Integer> mode = new ArrayList<>();
88+
for (int key : map.keySet()) {
89+
if (map.get(key) == modeCount) {
90+
mode.add(key);
91+
}
92+
}
93+
int[] result = new int[mode.size()];
94+
for (int i = 0; i < mode.size(); i++) {
95+
result[i] = mode.get(i);
7096
}
97+
return result;
7198
}
7299

73-
return list;
100+
private void dfs(TreeNode root, Map<Integer, Integer> map) {
101+
if (root == null) {
102+
return;
103+
}
104+
dfs(root.left, map);
105+
map.put(root.val, map.getOrDefault(root.val, 0) + 1);
106+
dfs(root.right, map);
107+
}
74108
}
75109

76110
}

src/test/java/com/fishercoder/_501Test.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
* Created by fishercoder on 1/28/17.
1414
*/
1515
public class _501Test {
16-
private static _501 test;
16+
private static _501.Solution1 solution1;
17+
private static _501.Solution2 solution2;
1718
private static int[] expected;
1819
private static int[] actual;
1920
private static TreeNode treeNode;
2021

2122
@BeforeClass
2223
public static void setup() {
23-
test = new _501();
24+
solution1 = new _501.Solution1();
25+
solution2 = new _501.Solution2();
2426
}
2527

2628
@Before
@@ -32,15 +34,16 @@ public void setupForEachTest() {
3234

3335
@Test
3436
public void test1() {
35-
3637
treeNode = new TreeNode(1);
3738
treeNode.right = new TreeNode(2);
3839
treeNode.right.left = new TreeNode(2);
3940
expected = new int[]{2};
4041
CommonUtils.printArray(expected);
4142
CommonUtils.printArray(actual);
42-
actual = test.findMode(treeNode);
43+
actual = solution1.findMode(treeNode);
4344
assertArrayEquals(expected, actual);
4445

46+
actual = solution2.findMode(treeNode);
47+
assertArrayEquals(expected, actual);
4548
}
4649
}

0 commit comments

Comments
 (0)