Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- TreeNode* sufficientSubset(TreeNode* root, int limit, int sum = 0) {
- if(root == nullptr){
- return nullptr;
- }
- // nsum = 5 + 4
- int nsum = sum + root->val;
- if(root->left == nullptr && root->right == nullptr){
- // leaf node hai ki nahi
- if(nsum < limit){
- return nullptr;
- }
- else{
- return root;
- }
- }
- else{
- root->left = sufficientSubset(root->left,limit,nsum);
- root->right = sufficientSubset(root->right,limit,nsum);
- if(root->left == nullptr && root->right == nullptr){
- return nullptr;
- }
- else{
- return root;
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment