Skip to content

Commit 15934f4

Browse files
committed
Add solution #1080
1 parent 42091e9 commit 15934f4

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,127 LeetCode solutions in JavaScript
1+
# 1,128 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -867,6 +867,7 @@
867867
1074|[Number of Submatrices That Sum to Target](./solutions/1074-number-of-submatrices-that-sum-to-target.js)|Hard|
868868
1078|[Occurrences After Bigram](./solutions/1078-occurrences-after-bigram.js)|Easy|
869869
1079|[Letter Tile Possibilities](./solutions/1079-letter-tile-possibilities.js)|Medium|
870+
1080|[Insufficient Nodes in Root to Leaf Paths](./solutions/1080-insufficient-nodes-in-root-to-leaf-paths.js)|Medium|
870871
1081|[Smallest Subsequence of Distinct Characters](./solutions/1081-smallest-subsequence-of-distinct-characters.js)|Medium|
871872
1092|[Shortest Common Supersequence](./solutions/1092-shortest-common-supersequence.js)|Hard|
872873
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1080. Insufficient Nodes in Root to Leaf Paths
3+
* https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/
4+
* Difficulty: Medium
5+
*
6+
* Given the root of a binary tree and an integer limit, delete all insufficient nodes in the
7+
* tree simultaneously, and return the root of the resulting binary tree.
8+
*
9+
* A node is insufficient if every root to leaf path intersecting this node has a sum strictly
10+
* less than limit.
11+
*
12+
* A leaf is a node with no children.
13+
*/
14+
15+
/**
16+
* Definition for a binary tree node.
17+
* function TreeNode(val, left, right) {
18+
* this.val = (val===undefined ? 0 : val)
19+
* this.left = (left===undefined ? null : left)
20+
* this.right = (right===undefined ? null : right)
21+
* }
22+
*/
23+
/**
24+
* @param {TreeNode} root
25+
* @param {number} limit
26+
* @return {TreeNode}
27+
*/
28+
var sufficientSubset = function(root, limit) {
29+
return checkPath(root, 0) ? root : null;
30+
31+
function checkPath(node, sum) {
32+
if (!node) return false;
33+
if (!node.left && !node.right) return sum + node.val >= limit;
34+
35+
const leftValid = checkPath(node.left, sum + node.val);
36+
const rightValid = checkPath(node.right, sum + node.val);
37+
38+
if (!leftValid) node.left = null;
39+
if (!rightValid) node.right = null;
40+
41+
return leftValid || rightValid;
42+
}
43+
};

0 commit comments

Comments
 (0)