Medium
Insufficient Nodes in Root to Leaf Paths — C++
Full explanation · Time O(n) · Space O(h)
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* sufficientSubset(TreeNode* root, int limit) {
if (!root) {
return nullptr;
}
if (!root->left && !root->right) {
return root->val < limit ? nullptr : root;
}
root->left = sufficientSubset(root->left, limit - root->val);
root->right = sufficientSubset(root->right, limit - root->val);
if (!root->left && !root->right) {
return nullptr;
}
return root;
}
};