Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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) {
- result.clear();
- m.clear();
- most=0;
- if(root==nullptr)
- return result;
- t(root);
- for(auto&& p:m)
- {
- if(p.second==most)
- result.push_back(p.first);
- }
- return result;
- }
- void t(TreeNode* node)
- {
- if(node==nullptr)
- return;
- ++m[node->val];
- most=max(most,m[node->val]);
- t(node->left);
- t(node->right);
- }
- vector<int> result;
- int most;
- unordered_map<int,int> m;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement