/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int count;
int maxcount;
TreeNode *pre;
vector<int> v;
vector<int> findMode(TreeNode* root) {
count = 0;
maxcount = 0;
pre = nullptr;
midsearch(root);
return v;
}
void midsearch(TreeNode* cur)
{
if(!cur) return;
midsearch(cur->left);
if(!pre) count = 1;
else if(pre->val == cur->val) count++;
else count = 1;
pre = cur;
if(count == maxcount) v.push_back(cur->val);
if(count > maxcount)
{
maxcount = count;
v.clear();
v.push_back(cur->val);
}
midsearch(cur->right);
return;
}
};
leetcode每日一题第135天
最新推荐文章于 2024-09-14 18:52:56 发布