Skip to content

Commit 88486d2

Browse files
committed
fd
1 parent c34f9b1 commit 88486d2

File tree

2 files changed

+105
-41
lines changed

2 files changed

+105
-41
lines changed

solution/src/main/java/com/inuker/solution/FindModeInBinarySearchTree.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313
/**
1414
* 这题是要找BST中出现次数最多的节点集合,这里允许有重复节点
1515
* 思路很简单,中序遍历,会按升序排列,再统计重复的值
16+
* 但是这样会消耗额外空间,对于类似于1,2,3,4...n-1, n-1,这种,所有的元素都只出现1次,
17+
* 唯独n-1出现了两次,这样mList中要保存整棵树的元素,所以空间是O(n),而返回值事实上只有一个
18+
* 这是分配在堆上的而非栈上,不能忽略。
19+
*
20+
* 如果额外空间要求为O(l),则分开两次扫描,第一次扫描最大次数,不记录最大次数的元素集合
21+
* 第二次根据最大次数来收集元素
1622
*/
1723
public class FindModeInBinarySearchTree {
1824

25+
/**
26+
* 这题类似于在一个有序的序列中查找频率最高的元素集合
27+
*/
28+
1929
private List<Integer> mList;
2030

2131
private int mCurCount;
@@ -61,4 +71,51 @@ private void inorderTraverse(TreeNode root) {
6171

6272
inorderTraverse(root.right);
6373
}
74+
75+
76+
/**
77+
* 如下是空间为O(l)的写法
78+
*/
79+
int mCurVal;
80+
int mMaxElemSize;
81+
int idx;
82+
int[] modes;
83+
84+
public int[] findMode2(TreeNode root) {
85+
helper(root);
86+
modes = new int[mMaxElemSize];
87+
mCurCount = 0;
88+
helper(root);
89+
return modes;
90+
}
91+
92+
private void helper(TreeNode node) {
93+
if (node == null) {
94+
return;
95+
}
96+
97+
helper(node.left);
98+
99+
if (node.val == mCurVal) {
100+
mCurCount++;
101+
} else {
102+
mCurCount = 1;
103+
mCurVal = node.val;
104+
}
105+
106+
if (modes == null) {
107+
if (mCurCount == mMaxCount) {
108+
mMaxElemSize++;
109+
} else if (mCurCount > mMaxCount) {
110+
mMaxElemSize = 1;
111+
mMaxCount = mCurCount;
112+
}
113+
} else {
114+
if (mCurCount == mMaxCount) {
115+
modes[idx++] = mCurVal;
116+
}
117+
}
118+
119+
helper(node.right);
120+
}
64121
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.inuker.solution.InorderSuccessorInBST;
66
import com.leetcode.library.TreeNode;
77

8+
import java.util.ArrayList;
89
import java.util.LinkedList;
910
import java.util.List;
1011
import java.util.Stack;
@@ -22,56 +23,62 @@ public static void main(String[] args) {
2223
}
2324
}
2425

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;
3830

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;
5232

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;
5439
}
5540

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;
5952
}
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;
6356
}
64-
return ret;
57+
58+
unite(node.right, result);
6559
}
6660

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;
7064
}
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;
7473
}
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);
7683
}
7784
}

0 commit comments

Comments
 (0)