leetcode

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

1448.cpp (639B)


      1 class Solution {
      2   public:
      3     int goodNodes(TreeNode *root) {
      4         queue<pair<TreeNode *, int>> q({{root, INT_MIN}});
      5         int res = 0;
      6 
      7         while (!q.empty()) {
      8             const auto [root, maxi] = q.front();
      9             q.pop();
     10             if (root->val >= maxi) res++;
     11             if (root->left) q.push({root->left, max(maxi, root->val)});
     12             if (root->right) q.push({root->right, max(maxi, root->val)});
     13 
     14             // For some reason it increases runtime and decreases memory usage
     15             // Must be leetcode error...
     16             root->left = root->right = nullptr;
     17         }
     18         return res;
     19     }
     20 };