-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1080.java
27 lines (24 loc) · 1009 Bytes
/
_1080.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.fishercoder.solutions.secondthousand;
import com.fishercoder.common.classes.TreeNode;
public class _1080 {
public static class Solution1 {
/*
* Credit: https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/solutions/1340243/concise-dfs-solution-cpp-and-java-0ms/
* DFS does this very cleanly.
*/
public TreeNode sufficientSubset(TreeNode root, int limit) {
return dfs(root, limit, 0);
}
private TreeNode dfs(TreeNode root, int limit, int sumThusFar) {
if (root == null) {
return null;
}
if (root.left == null && root.right == null) {
return root.val + sumThusFar < limit ? null : root;
}
root.left = dfs(root.left, limit, sumThusFar + root.val);
root.right = dfs(root.right, limit, sumThusFar + root.val);
return root.left == null && root.right == null ? null : root;
}
}
}