题目描述
LeetCode 501
样例
待添加
算法
待修改,暂略
C++ 代码
class Solution {
public:
vector<int> ans;
int maxCnt = 0,curCnt = 0,last;
vector<int> findMode(TreeNode* root) {
dfs(root);
return ans;
}
void dfs(TreeNode* root) {
if(!root) return;
dfs(root->left);
if(!curCnt || root->val == last) {
curCnt++;
last = root->val;
} else {
curCnt = 1;
last = root->val;
}
if(curCnt > maxCnt) {
ans = {last};
maxCnt = curCnt;
} else if(curCnt == maxCnt) ans.push_back(last);
dfs(root->right);
}
};