Talentd
FleetCode

1080. Insufficient Nodes in Root to Leaf Paths

Medium55.6% acceptance1 company

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.

Example 1:

Input: root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1
Output: [1,2,3,4,null,null,7,8,9,null,14]

Example 2:

Input: root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22
Output: [5,4,8,11,null,17,4,7,null,null,null,5]

Example 3:

Input: root = [1,2,-3,-5,null,4,null], limit = -1
Output: [1,null,-3,4]

Constraints:

  • The number of nodes in the tree is in the range [1, 5000].
  • -105 <= Node.val <= 105
  • -109 <= limit <= 109

Asked at 1 company

Hints open one at a time

Hint 1
Consider a DFS traversal of the tree. You can keep track of the current path sum from root to this node, and you can also use DFS to return the maximum value of any path from this node to the leaf. This will tell you if this node is insufficient.
11:11
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1# Definition for a binary tree node.
2# class TreeNode:
3# def __init__(self, val=0, left=None, right=None):
4# self.val = val
5# self.left = left
6# self.right = right
7class Solution:
8 def sufficientSubset(self, root: Optional[TreeNode], limit: int) -> Optional[TreeNode]:
9
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14]
1
Input
[5,4,8,11,null,17,4,7,1,null,null,5,3]
22
Input
[1,2,-3,-5,null,4,null]
-1
Your code is saved on this device.Solution