Skip to content

Commit e908f85

Browse files
StoneLyuazl397985856
authored andcommitted
1 parent c5074ce commit e908f85

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The data structures mainly includes:
139139
- [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
140140
- [0437.path-sum-iii](./problems/437.path-sum-iii.md) 🆕
141141
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
142+
- [0501.find-mode-in-binary-search-tree](./problems/501.Find-Mode-in-Binary-Search-Tree.md) 🆕
142143
- [0575.distribute-candies](./problems/575.distribute-candies.md)
143144

144145
#### Medium

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
142142
- [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
143143
- [0437.path-sum-iii](./problems/437.path-sum-iii.md) 🆕
144144
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
145+
- [0501.find-mode-in-binary-search-tree](./problems/501.Find-Mode-in-Binary-Search-Tree.md)🆕
145146
- [0575.distribute-candies](./problems/575.distribute-candies.md)
146147

147148
#### 中等难度
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
## Problem on Leetcode
2+
3+
https://leetcode.com/problems/find-mode-in-binary-search-tree/
4+
5+
## Description
6+
7+
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.
8+
9+
Assume a BST is defined as follows:
10+
11+
- The left subtree of a node contains only nodes with keys less than or equal to the node's key.
12+
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
13+
- Both the left and right subtrees must also be binary search trees.
14+
15+
For example:
16+
Given BST `[1,null,2,2]`,
17+
18+
```bash
19+
1
20+
\
21+
2
22+
/
23+
2
24+
```
25+
26+
return `[2]`.
27+
28+
Note: If a tree has more than one mode, you can return them in any order.
29+
30+
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
31+
32+
## Ideas
33+
34+
Basically, it needs traversing, counting and recording. `map` can be used to help us to do record and count.
35+
For doing that without using any extra space, the property of BST will be used. For each node, the value of the left child is no greater than its value, while the value of right child is no less than the its value. So, when traversing each node, only the value of previous node is required to be compared with the value of current node for counting.
36+
As the problem shown, an array of intergers will be returned. While the number of modes is unknown, using `ArrayList` to store all outputs is a good choice because `ArrayList` can be converted into array conveniently.
37+
38+
## Codes
39+
40+
Supported Language: `Java`
41+
42+
- Java Code:
43+
44+
```java
45+
/**
46+
* Definition for a binary tree node.
47+
* public class TreeNode {
48+
* int val;
49+
* TreeNode left;
50+
* TreeNode right;
51+
* TreeNode(int x) { val = x; }
52+
* }
53+
*/
54+
class Solution {
55+
List<Integer> list = new ArrayList<> ();
56+
TreeNode preNode = null;
57+
int max = 0, count = 0;
58+
59+
public int[] findMode(TreeNode root) {
60+
helper(root);
61+
int[] res = new int[list.size()];
62+
for (int i=0; i<res.length; i++) {
63+
res[i] = list.get(i);
64+
}
65+
return res;
66+
}
67+
68+
private void helper (TreeNode root) {
69+
if (root == null) return;
70+
helper(root.left);
71+
72+
if (preNode != null && root.val == preNode.val) {
73+
count++;
74+
} else {
75+
count = 1;
76+
}
77+
78+
if (count > max) {
79+
list.clear();
80+
list.add(root.val);
81+
max = count;
82+
} else if (max == count) {
83+
list.add(root.val);
84+
}
85+
preNode = root;
86+
helper(root.right);
87+
}
88+
}
89+
```

0 commit comments

Comments
 (0)