Medium
Given the root
of a binary tree and an integer limit
, delete all insufficient nodes in the tree simultaneously, and return the root of the resulting binary tree.
A node is insufficient if every root to leaf path intersecting this node has a sum strictly less than limit
.
A leaf is a node with no children.
Example 1:
Input: root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1
Output: [1,2,3,4,null,null,7,8,9,null,14]
Example 2:
Input: root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22
Output: [5,4,8,11,null,17,4,7,null,null,null,5]
Example 3:
Input: root = [1,2,-3,-5,null,4,null], limit = -1
Output: [1,null,-3,4]
Constraints:
[1, 5000]
.-105 <= Node.val <= 105
-109 <= limit <= 109
import com_github_leetcode.TreeNode;
/*
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode sufficientSubset(TreeNode root, int limit) {
return sufficientSubset(root, limit, 0, root.left == null && root.right == null) < limit
? null
: root;
}
public int sufficientSubset(TreeNode root, int limit, int sum, boolean isLeaf) {
if (root != null) {
int leftSum =
sufficientSubset(
root.left,
limit,
sum + root.val,
root.left == null && root.right == null);
int rightSum =
sufficientSubset(
root.right,
limit,
sum + root.val,
root.left == null && root.right == null);
if (leftSum < limit) {
root.left = null;
}
if (rightSum < limit) {
root.right = null;
}
return Math.max(leftSum, rightSum);
}
return isLeaf ? sum : Integer.MIN_VALUE;
}
}