当前位置:网站首页>【LeetCode 42】501. Mode in binary search tree
【LeetCode 42】501. Mode in binary search tree
2022-07-04 08:51:00 【CodeLinghu】
【LeetCode 42】501. The mode in the binary search tree
List of articles
One 、 The question
Two 、 The answer process
stay 《 Binary tree : The minimum absolute value difference of the search tree 》 We used pre
Pointers and cur
Skills of pointer . Here again .
Make a pointer to the previous node , So every time cur( Current node ) Talent and pre( Previous node ) The comparison .
And when initializing pre==NULL, So when pre by NULL when , We know that this is the first element of comparison .
if (pre == NULL) {
// First node
count = 1; // The frequency is 1
} else if (pre->val == cur->val) {
// Same value as the previous node
count++;
} else {
// Different from the previous node value
count = 1;
}
pre = cur; // Update previous node
How to traverse once to find all modes ?
Traversal frequency count Greater than maxCount When , Not only to update maxCount, And empty the result set ( The following code is result Array ), Because the elements before the result set are invalid .
if (count > maxCount) {
// If the count is greater than the maximum
maxCount = count; // Update maximum frequency
result.clear(); // A crucial step , Don't forget to empty result, Before result All the elements in the have failed
result.push_back(cur->val);
}
class Solution {
public:
int maxCount;// Maximum frequency
int count;// Statistical frequency
TreeNode* pre;
vector<int>result;
void searchBST(TreeNode *cur)
{
if(cur==NULL) return;
searchBST(cur->left);// Left
if(pre==NULL)// The first node
{
count=1;
}else if(pre->val==cur->val){
// Same value as the previous node
count++;
} else{
// Different from the previous node value
count=1;
}
pre=cur;// Update previous node
if(count==maxCount)// If it is the same as the maximum , In the result
{
result.push_back(cur->val);
}
if(count>maxCount)// If the count is greater than the maximum frequency
{
maxCount=count;// Update maximum frequency
result.clear();// Key step , Don't forget to empty result, Before result All the elements in the have failed
result.push_back(cur->val);
}
searchBST(cur->right);// Right
return;
}
vector<int> findMode(TreeNode* root) {
count=0;
maxCount=0;
TreeNode* pre=NULL;// Record the previous node
result.clear();
searchBST(root);
return result;
}
};
边栏推荐
- ctfshow web255 web 256 web257
- awk从入门到入土(15)awk执行外部命令
- C语言-入门-基础-语法-[运算符,类型转换](六)
- Leetcode topic [array] - 121 - the best time to buy and sell stocks
- ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
- Getting started with microservices: gateway gateway
- 2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
- 没有Kubernetes怎么玩Dapr?
- 随机事件的关系与运算
- Educational Codeforces Round 115 (Rated for Div. 2)
猜你喜欢
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Relationship and operation of random events
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Display Chinese characters according to numbers
How to solve the problem that computers often flash
Codeforces Global Round 21(A-E)
How to choose solid state hard disk and mechanical hard disk in computer
What if I forget the router password
C语言-入门-基础-语法-[运算符,类型转换](六)
[CV] Wu Enda machine learning course notes | Chapter 9
随机推荐
What if the wireless network connection of the laptop is unavailable
AcWing 244. Enigmatic cow (tree array + binary search)
awk从入门到入土(4)用户自定义变量
Live in a dream, only do things you don't say
09 softmax regression + loss function
[untitled] forwarding least square method
LinkedList in the list set is stored in order
【无标题】转发最小二乘法
Comparison between sentinel and hystrix
[Chongqing Guangdong education] National Open University spring 2019 455 logistics practice reference questions
1211 or chicken and rabbit in the same cage
How to send pictures to the server in the form of file stream through the upload control of antd
Display Chinese characters according to numbers
How to choose solid state hard disk and mechanical hard disk in computer
A subclass must use the super keyword to call the methods of its parent class
C语言-入门-基础-语法-数据类型(四)
Cancel ctrl+alt+delete when starting up
Educational Codeforces Round 119 (Rated for Div. 2)
Educational Codeforces Round 115 (Rated for Div. 2)
上周热点回顾(6.27-7.3)