版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
抖机灵
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<int>ans;
int count = -1;
int max = 0;
int tmp;
public:
vector<int> findMode(TreeNode* root) {
find(root);
return ans;
}
void find(TreeNode* root)
{
if(root)
{
find(root->left);
if(count == -1)
{
count = 1;
max = count;
ans.push_back(root->val);
}
else
{
if(tmp == root->val) count++;
else count = 1;
if(count >max)
{
max = count;
ans.clear();
ans.push_back(root->val);
}
else if(count == max)
{
ans.push_back(root->val);
}
}
tmp = root->val;
find(root->right);
}
}
};