jibha

Untitled

Jan 30th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 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. int ans=0;
  16.  
  17.  
  18. bool checkPal(map<int,int> m){
  19.  
  20. int odd=0;
  21. int even=0;
  22.  
  23. for(auto iter:m){
  24.  
  25. if(iter.second%2!=0){
  26. odd++;
  27. }else{
  28. even++;
  29. }
  30. }
  31.  
  32. return odd<=1;
  33.  
  34.  
  35. }
  36.  
  37. void dfs(TreeNode* root,map<int,int> m){
  38.  
  39. if(root==nullptr){
  40. return;
  41. }
  42. m[root->val]++;
  43. if(root->left==nullptr&&root->right==nullptr){
  44. if(checkPal(m)){
  45. ans++;
  46. }
  47. }
  48. dfs(root->left,m);
  49. dfs(root->right,m);
  50.  
  51. return;
  52.  
  53.  
  54. }
  55.  
  56.  
  57. int pseudoPalindromicPaths (TreeNode* root) {
  58. map<int,int> m;
  59. m.clear();
  60.  
  61.  
  62. dfs(root,m);
  63.  
  64.  
  65. return ans;
  66.  
  67.  
  68.  
  69. }
  70. };
Advertisement
Add Comment
Please, Sign In to add comment