leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1457.cpp (569B)


      1 
      2 class Solution {
      3     int count[10] = {0};
      4 
      5   public:
      6     int pseudoPalindromicPaths(TreeNode *root) {
      7         int res = 0;
      8 
      9         count[root->val]++;
     10         if (!root->left && !root->right) {
     11             int odd = 0;
     12             for (int i = 1; i <= 9; i++)
     13                 if (count[i] % 2 && odd++) break;
     14             res = odd <= 1;
     15         } else {
     16             if (root->left) res += pseudoPalindromicPaths(root->left);
     17             if (root->right) res += pseudoPalindromicPaths(root->right);
     18         }
     19         count[root->val]--;
     20 
     21         return res;
     22     }
     23 };