# LEETCODE 1080. Insufficient Nodes in Root to Leaf Paths 解题思路分析

`输入：root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1`
`输出：[1,2,3,4,null,null,7,8,9,null,14]`

`输入：root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22`
`输出：[5,4,8,11,null,17,4,7,null,null,null,5]`

```输入：root = [5,-6,-6], limit = 0

1. 给定的树有 `1` 到 `5000` 个节点
2. `-10^5 <= node.val <= 10^5`
3. `-10^9 <= limit <= 10^9`

```public TreeNode sufficientSubset(TreeNode root, int limit) {
return help(root, limit, 0);
}

TreeNode help(TreeNode root, int limit, int sum){
if(root == null){ // 当前节点为空，返回空
return null;
}
if(root.right==null && root.left==null){ // 叶子节点
return sum + root.val < limit ? null : root; // 判断该路径和是否合法，非法时返回空
}
// 非叶子节点
TreeNode left = help(root.left, limit, sum + root.val); // 递归得到左节点
TreeNode right = help(root.right, limit, sum + root.val); // 递归得到右节点
root.left=left;
root.right=right;
if(left==right){ // 左右节点均为空，说明无合法路径，返回空
return null;
}else{
return root; // 存在合法路径，返回当前节点
}
}```

Runtime: 1 ms, faster than 100.00% of Java online submissions for Insufficient Nodes in Root to Leaf Paths.

Memory Usage: 38.5 MB, less than 100.00% of Java online submissions for Insufficient Nodes in Root to Leaf Paths.