Insufficient Nodes in Root to Leaf Paths
https://leetcode.com/contest/weekly-contest-140/problems/insufficient-nodes-in-root-to-leaf-paths/
Last updated
https://leetcode.com/contest/weekly-contest-140/problems/insufficient-nodes-in-root-to-leaf-paths/
Last updated
class Solution {
public:
TreeNode* sufficientSubset(TreeNode* root, int limit) {
if (root == NULL) return root;
// If it is a leaf, check whether val satisfies the limit. Return null => del this node.
if (root->left == root->right) return root->val < limit ? NULL : root;
// Divide and conquer.
root->left = sufficientSubset(root->left, limit - root->val);
root->right = sufficientSubset(root->right, limit - root->val);
// If it becomes a new leaf, return null.
return root->left == root->right ? NULL : root;
}
};