Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 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(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<int> findMode(TreeNode* root) {
  13. result.clear();
  14. m.clear();
  15. most=0;
  16. if(root==nullptr)
  17. return result;
  18.  
  19. t(root);
  20. for(auto&& p:m)
  21. {
  22. if(p.second==most)
  23. result.push_back(p.first);
  24. }
  25. return result;
  26. }
  27.  
  28. void t(TreeNode* node)
  29. {
  30. if(node==nullptr)
  31. return;
  32. ++m[node->val];
  33. most=max(most,m[node->val]);
  34. t(node->left);
  35. t(node->right);
  36. }
  37. vector<int> result;
  38. int most;
  39. unordered_map<int,int> m;
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement