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)


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