Skip to content

Commit 71c79d1

Browse files
committed
Runtime: 160 ms (Top 41.18%) | Memory: 49.6 MB (Top 38.24%)
1 parent 85a6cea commit 71c79d1

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
// Runtime: 160 ms (Top 41.18%) | Memory: 49.6 MB (Top 38.24%)
12
var sufficientSubset = function(root, limit) {
23
const MIN = Number.MIN_SAFE_INTEGER;
34
const sum = removeNodes(root, 0);
4-
5+
56
if (sum < limit) return null;
6-
7+
78
return root;
8-
9+
910
function removeNodes(node, prevSum) {
1011
if (node == null) return MIN;
1112
if (node.left == node.right) return node.val + prevSum;
12-
13+
1314
const leftSum = removeNodes(node.left, prevSum + node.val);
1415
const rightSum = removeNodes(node.right, prevSum + node.val);
15-
16+
1617
if (leftSum < limit) node.left = null;
1718
if (rightSum < limit) node.right = null;
18-
19+
1920
if (node.left == node.right) return MIN;
20-
21+
2122
return node.left == null ? rightSum : leftSum;
2223
}
23-
};
24+
};

0 commit comments

Comments
 (0)