-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathFind Mode in Binary Search Tree.cpp
More file actions
38 lines (38 loc) · 1.31 KB
/
Copy pathFind Mode in Binary Search Tree.cpp
File metadata and controls
38 lines (38 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 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 {
public:
vector<int> findMode(TreeNode* root) {
if (!root) return {};
vector<int> res;
TreeNode *now = root, *pre = NULL;
stack<TreeNode*> s;
int mx = 0, cnt = 1;
while (!s.empty() || now) {
while (now) { //中序遍历,左中右。将每个结点的左子树入栈
s.push(now);
now = now->left;
}
now = s.top(); s.pop(); //取栈顶元素
if (pre) { //判断当前元素和上一个元素值是否一样,一样cnt计数加一
cnt = (now->val == pre->val) ? cnt + 1 : 1;
} //如果cnt大于等于mx,说明当前元素重复次数大于之前最大的重复元素的次数,需要将新结果入结果栈。
if (cnt >= mx) {
if (cnt > mx) res.clear();
res.push_back(now->val);
mx = cnt;
}
if (!pre) pre = new TreeNode(now->val);
pre->val = now->val;
now = now->right;
}
return res;
}
};