-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1080-insufficient-nodes-in-root-to-leaf-paths.js
43 lines (38 loc) · 1.25 KB
/
1080-insufficient-nodes-in-root-to-leaf-paths.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 1080. Insufficient Nodes in Root to Leaf Paths
* https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/
* Difficulty: Medium
*
* Given the root of a binary tree and an integer limit, delete all insufficient nodes in the
* tree simultaneously, and return the root of the resulting binary tree.
*
* A node is insufficient if every root to leaf path intersecting this node has a sum strictly
* less than limit.
*
* A leaf is a node with no children.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} limit
* @return {TreeNode}
*/
var sufficientSubset = function(root, limit) {
return checkPath(root, 0) ? root : null;
function checkPath(node, sum) {
if (!node) return false;
if (!node.left && !node.right) return sum + node.val >= limit;
const leftValid = checkPath(node.left, sum + node.val);
const rightValid = checkPath(node.right, sum + node.val);
if (!leftValid) node.left = null;
if (!rightValid) node.right = null;
return leftValid || rightValid;
}
};