Advertisement
jibha

Untitled

Jan 23rd, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14.  
  15. map<int,int> m;
  16.  
  17. vector<int> ans(){
  18.  
  19. int maxi=0;
  20. for(auto i:m){
  21. maxi=max(maxi,i.second);
  22. }
  23. vector<int> ret;
  24. for(auto i:m){
  25. if(i.second==maxi){
  26. ret.push_back(i.first);
  27. }
  28. }
  29.  
  30. return ret;
  31.  
  32. }
  33.  
  34. vector<int> findMode(TreeNode* root) {
  35.  
  36. if(root==nullptr){
  37. return {};
  38. }
  39. m[root->val]++;
  40.  
  41. findMode(root->left);
  42. findMode(root->right);
  43.  
  44.  
  45. return ans();
  46. }
  47. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement