Insufficient Nodes in Root to Leaf Paths LeetCode solution in Java

1
class Solution {
2
public TreeNode helper(TreeNode root, int limit, int sumTillNow) {
3
if (root == null) return null;
4

5
if (root.left == null && root.right == null) return root.val + sumTillNow < limit ? null : root;
6

7
root.left = helper(root.left, limit, sumTillNow + root.val);
8
root.right = helper(root.right, limit, sumTillNow + root.val);
9

10
return root.left == null && root.right == null ? null : root;
11
}
12

13
public TreeNode sufficientSubset(TreeNode root, int limit) {
14
return helper(root, limit, 0);
15
}
16
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0