Skip to content

Commit 48bb051

Browse files
committed
feat: add solutions to lc problem: No.0501
No.0501.Find Mode in Binary Search Tree
1 parent ddb655f commit 48bb051

File tree

9 files changed

+442
-34
lines changed

9 files changed

+442
-34
lines changed

solution/0000-0099/0098.Validate Binary Search Tree/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
# self.right = right
6060
class Solution:
6161
def isValidBST(self, root: TreeNode) -> bool:
62-
prev = float('-inf')
63-
6462
def dfs(root):
6563
nonlocal prev
6664
if root is None:
@@ -74,6 +72,7 @@ class Solution:
7472
return False
7573
return True
7674

75+
prev = float('-inf')
7776
return dfs(root)
7877
```
7978

solution/0000-0099/0098.Validate Binary Search Tree/README_EN.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
# self.right = right
5454
class Solution:
5555
def isValidBST(self, root: TreeNode) -> bool:
56-
prev = float('-inf')
57-
5856
def dfs(root):
5957
nonlocal prev
6058
if root is None:
@@ -68,6 +66,7 @@ class Solution:
6866
return False
6967
return True
7068

69+
prev = float('-inf')
7170
return dfs(root)
7271
```
7372

solution/0000-0099/0098.Validate Binary Search Tree/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
# self.right = right
77
class Solution:
88
def isValidBST(self, root: TreeNode) -> bool:
9-
prev = float('-inf')
10-
119
def dfs(root):
1210
nonlocal prev
1311
if root is None:
@@ -21,4 +19,5 @@ def dfs(root):
2119
return False
2220
return True
2321

22+
prev = float('-inf')
2423
return dfs(root)

solution/0500-0599/0501.Find Mode in Binary Search Tree/README.md

+157-1
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,178 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39+
中序遍历。其中,mx 表示最大频数,cnt 表示上一个元素出现的次数,prev 表示上一个元素,ans 表示结果列表。
40+
3941
<!-- tabs:start -->
4042

4143
### **Python3**
4244

4345
<!-- 这里可写当前语言的特殊实现逻辑 -->
4446

4547
```python
46-
48+
# Definition for a binary tree node.
49+
# class TreeNode:
50+
# def __init__(self, val=0, left=None, right=None):
51+
# self.val = val
52+
# self.left = left
53+
# self.right = right
54+
class Solution:
55+
def findMode(self, root: TreeNode) -> List[int]:
56+
def dfs(root):
57+
if root is None:
58+
return
59+
nonlocal mx, prev, ans, cnt
60+
dfs(root.left)
61+
cnt = cnt + 1 if prev == root.val else 1
62+
if cnt > mx:
63+
ans = [root.val]
64+
mx = cnt
65+
elif cnt == mx:
66+
ans.append(root.val)
67+
prev = root.val
68+
dfs(root.right)
69+
70+
prev = None
71+
mx = cnt = 0
72+
ans = []
73+
dfs(root)
74+
return ans
4775
```
4876

4977
### **Java**
5078

5179
<!-- 这里可写当前语言的特殊实现逻辑 -->
5280

5381
```java
82+
/**
83+
* Definition for a binary tree node.
84+
* public class TreeNode {
85+
* int val;
86+
* TreeNode left;
87+
* TreeNode right;
88+
* TreeNode() {}
89+
* TreeNode(int val) { this.val = val; }
90+
* TreeNode(int val, TreeNode left, TreeNode right) {
91+
* this.val = val;
92+
* this.left = left;
93+
* this.right = right;
94+
* }
95+
* }
96+
*/
97+
class Solution {
98+
private int mx;
99+
private int cnt;
100+
private TreeNode prev;
101+
private List<Integer> res;
102+
103+
public int[] findMode(TreeNode root) {
104+
res = new ArrayList<>();
105+
dfs(root);
106+
int[] ans = new int[res.size()];
107+
for (int i = 0; i < res.size(); ++i) {
108+
ans[i] = res.get(i);
109+
}
110+
return ans;
111+
}
112+
113+
private void dfs(TreeNode root) {
114+
if (root == null) {
115+
return;
116+
}
117+
dfs(root.left);
118+
cnt = prev != null && prev.val == root.val ? cnt + 1 : 1;
119+
if (cnt > mx) {
120+
res = new ArrayList<>(Arrays.asList(root.val));
121+
mx = cnt;
122+
} else if (cnt == mx) {
123+
res.add(root.val);
124+
}
125+
prev = root;
126+
dfs(root.right);
127+
}
128+
}
129+
```
130+
131+
### **C++**
132+
133+
```cpp
134+
/**
135+
* Definition for a binary tree node.
136+
* struct TreeNode {
137+
* int val;
138+
* TreeNode *left;
139+
* TreeNode *right;
140+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
141+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
142+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
143+
* };
144+
*/
145+
class Solution {
146+
public:
147+
TreeNode* prev;
148+
int mx, cnt;
149+
vector<int> ans;
150+
151+
vector<int> findMode(TreeNode* root) {
152+
dfs(root);
153+
return ans;
154+
}
155+
156+
void dfs(TreeNode* root) {
157+
if (!root) return;
158+
dfs(root->left);
159+
cnt = prev != nullptr && prev->val == root->val ? cnt + 1 : 1;
160+
if (cnt > mx)
161+
{
162+
ans.clear();
163+
ans.push_back(root->val);
164+
mx = cnt;
165+
}
166+
else if (cnt == mx) ans.push_back(root->val);
167+
prev = root;
168+
dfs(root->right);
169+
}
170+
};
171+
```
54172
173+
### **Go**
174+
175+
```go
176+
/**
177+
* Definition for a binary tree node.
178+
* type TreeNode struct {
179+
* Val int
180+
* Left *TreeNode
181+
* Right *TreeNode
182+
* }
183+
*/
184+
func findMode(root *TreeNode) []int {
185+
mx, cnt := 0, 0
186+
var prev *TreeNode
187+
var ans []int
188+
var dfs func(root *TreeNode)
189+
dfs = func(root *TreeNode) {
190+
if root == nil {
191+
return
192+
}
193+
dfs(root.Left)
194+
if prev != nil && prev.Val == root.Val {
195+
cnt++
196+
} else {
197+
cnt = 1
198+
}
199+
if cnt > mx {
200+
ans = []int{root.Val}
201+
mx = cnt
202+
} else if cnt == mx {
203+
ans = append(ans, root.Val)
204+
}
205+
prev = root
206+
dfs(root.Right)
207+
}
208+
dfs(root)
209+
return ans
210+
}
55211
```
56212

57213
### **...**

solution/0500-0599/0501.Find Mode in Binary Search Tree/README_EN.md

+155-1
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,167 @@
4949
### **Python3**
5050

5151
```python
52-
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, val=0, left=None, right=None):
55+
# self.val = val
56+
# self.left = left
57+
# self.right = right
58+
class Solution:
59+
def findMode(self, root: TreeNode) -> List[int]:
60+
def dfs(root):
61+
if root is None:
62+
return
63+
nonlocal mx, prev, ans, cnt
64+
dfs(root.left)
65+
cnt = cnt + 1 if prev == root.val else 1
66+
if cnt > mx:
67+
ans = [root.val]
68+
mx = cnt
69+
elif cnt == mx:
70+
ans.append(root.val)
71+
prev = root.val
72+
dfs(root.right)
73+
74+
prev = None
75+
mx = cnt = 0
76+
ans = []
77+
dfs(root)
78+
return ans
5379
```
5480

5581
### **Java**
5682

5783
```java
84+
/**
85+
* Definition for a binary tree node.
86+
* public class TreeNode {
87+
* int val;
88+
* TreeNode left;
89+
* TreeNode right;
90+
* TreeNode() {}
91+
* TreeNode(int val) { this.val = val; }
92+
* TreeNode(int val, TreeNode left, TreeNode right) {
93+
* this.val = val;
94+
* this.left = left;
95+
* this.right = right;
96+
* }
97+
* }
98+
*/
99+
class Solution {
100+
private int mx;
101+
private int cnt;
102+
private TreeNode prev;
103+
private List<Integer> res;
104+
105+
public int[] findMode(TreeNode root) {
106+
res = new ArrayList<>();
107+
dfs(root);
108+
int[] ans = new int[res.size()];
109+
for (int i = 0; i < res.size(); ++i) {
110+
ans[i] = res.get(i);
111+
}
112+
return ans;
113+
}
114+
115+
private void dfs(TreeNode root) {
116+
if (root == null) {
117+
return;
118+
}
119+
dfs(root.left);
120+
cnt = prev != null && prev.val == root.val ? cnt + 1 : 1;
121+
if (cnt > mx) {
122+
res = new ArrayList<>(Arrays.asList(root.val));
123+
mx = cnt;
124+
} else if (cnt == mx) {
125+
res.add(root.val);
126+
}
127+
prev = root;
128+
dfs(root.right);
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
/**
137+
* Definition for a binary tree node.
138+
* struct TreeNode {
139+
* int val;
140+
* TreeNode *left;
141+
* TreeNode *right;
142+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
143+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
144+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
145+
* };
146+
*/
147+
class Solution {
148+
public:
149+
TreeNode* prev;
150+
int mx, cnt;
151+
vector<int> ans;
152+
153+
vector<int> findMode(TreeNode* root) {
154+
dfs(root);
155+
return ans;
156+
}
157+
158+
void dfs(TreeNode* root) {
159+
if (!root) return;
160+
dfs(root->left);
161+
cnt = prev != nullptr && prev->val == root->val ? cnt + 1 : 1;
162+
if (cnt > mx)
163+
{
164+
ans.clear();
165+
ans.push_back(root->val);
166+
mx = cnt;
167+
}
168+
else if (cnt == mx) ans.push_back(root->val);
169+
prev = root;
170+
dfs(root->right);
171+
}
172+
};
173+
```
58174
175+
### **Go**
176+
177+
```go
178+
/**
179+
* Definition for a binary tree node.
180+
* type TreeNode struct {
181+
* Val int
182+
* Left *TreeNode
183+
* Right *TreeNode
184+
* }
185+
*/
186+
func findMode(root *TreeNode) []int {
187+
mx, cnt := 0, 0
188+
var prev *TreeNode
189+
var ans []int
190+
var dfs func(root *TreeNode)
191+
dfs = func(root *TreeNode) {
192+
if root == nil {
193+
return
194+
}
195+
dfs(root.Left)
196+
if prev != nil && prev.Val == root.Val {
197+
cnt++
198+
} else {
199+
cnt = 1
200+
}
201+
if cnt > mx {
202+
ans = []int{root.Val}
203+
mx = cnt
204+
} else if cnt == mx {
205+
ans = append(ans, root.Val)
206+
}
207+
prev = root
208+
dfs(root.Right)
209+
}
210+
dfs(root)
211+
return ans
212+
}
59213
```
60214

61215
### **...**

0 commit comments

Comments
 (0)