标签:http cto arch solution val int problems else ++
https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/
class Solution {
public:
int count = 0, max_count = 0;
TreeNode* pre = NULL;
vector<int> ans;
void dfs(TreeNode* node) {
if (node == NULL) return;
dfs(node->left);
if (pre == NULL) {
count++;
ans.push_back(node->val);
}
else if (pre->val == node->val) {
++count;
}
else if (pre->val != node->val) {
count = 1;
}
if (count == max_count) {
ans.push_back(node->val);
}
else if (count >= max_count) {
max_count = count;
ans.clear();
ans.push_back(node->val);
}
pre = node;
dfs(node->right);
return;
}
vector<int> findMode(TreeNode* root) {
if (root == NULL) {
return {};
}
dfs(root);
return ans;
}
};
501. Find Mode in Binary Search Tree
标签:http cto arch solution val int problems else ++
原文地址:https://www.cnblogs.com/Hunter01/p/13725091.html