02. Leaf under budget
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class Solution {
public:
int getCount(Node *root, int k) {
queue<pair<Node*, int>> q;
q.push({root, 1});
int cnt = 0;
while (!q.empty() && k > 0) {
auto node = q.front().first;
int level = q.front().second;
q.pop();
if (!node->left && !node->right) {
if (level > k)
return cnt;
++cnt;
k -= level;
} else {
if (node->left)
q.push({node->left, level + 1});
if (node->right)
q.push({node->right, level + 1});
}
}
return cnt;
}
};Contribution and Support
Last updated