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() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- map<int,int> m;
- vector<int> ans(){
- int maxi=0;
- for(auto i:m){
- maxi=max(maxi,i.second);
- }
- vector<int> ret;
- for(auto i:m){
- if(i.second==maxi){
- ret.push_back(i.first);
- }
- }
- return ret;
- }
- vector<int> findMode(TreeNode* root) {
- if(root==nullptr){
- return {};
- }
- m[root->val]++;
- findMode(root->left);
- findMode(root->right);
- return ans();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement