Forgetting null/base-case handling
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.
Move from brute-force thinking to an efficient approach using tree strategy.
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:
[1, 5000].-105 <= Node.val <= 105-109 <= limit <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Tree
[1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14] 1
[5,4,8,11,null,17,4,7,1,null,null,5,3] 22
[1,2,-3,-5,null,4,null] -1
count-nodes-equal-to-average-of-subtree)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1080: Insufficient Nodes in Root to Leaf Paths
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode sufficientSubset(TreeNode root, int limit) {
if (root == null) {
return null;
}
limit -= root.val;
if (root.left == null && root.right == null) {
return limit > 0 ? null : root;
}
root.left = sufficientSubset(root.left, limit);
root.right = sufficientSubset(root.right, limit);
return root.left == null && root.right == null ? null : root;
}
}
// Accepted solution for LeetCode #1080: Insufficient Nodes in Root to Leaf Paths
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func sufficientSubset(root *TreeNode, limit int) *TreeNode {
if root == nil {
return nil
}
limit -= root.Val
if root.Left == nil && root.Right == nil {
if limit > 0 {
return nil
}
return root
}
root.Left = sufficientSubset(root.Left, limit)
root.Right = sufficientSubset(root.Right, limit)
if root.Left == nil && root.Right == nil {
return nil
}
return root
}
# Accepted solution for LeetCode #1080: Insufficient Nodes in Root to Leaf Paths
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sufficientSubset(
self, root: Optional[TreeNode], limit: int
) -> Optional[TreeNode]:
if root is None:
return None
limit -= root.val
if root.left is None and root.right is None:
return None if limit > 0 else root
root.left = self.sufficientSubset(root.left, limit)
root.right = self.sufficientSubset(root.right, limit)
return None if root.left is None and root.right is None else root
// Accepted solution for LeetCode #1080: Insufficient Nodes in Root to Leaf Paths
struct Solution;
use rustgym_util::*;
trait Postorder {
fn postorder(self, limit: i32) -> TreeLink;
}
impl Postorder for TreeLink {
fn postorder(self, limit: i32) -> TreeLink {
if let Some(node) = self {
let val = node.borrow_mut().val;
let left = node.borrow_mut().left.take();
let right = node.borrow_mut().right.take();
if left.is_none() && right.is_none() {
if val >= limit {
Some(node)
} else {
None
}
} else {
let l = left.postorder(limit - val);
let r = right.postorder(limit - val);
if l.is_some() || r.is_some() {
node.borrow_mut().left = l;
node.borrow_mut().right = r;
Some(node)
} else {
None
}
}
} else {
None
}
}
}
impl Solution {
fn sufficient_subset(root: TreeLink, limit: i32) -> TreeLink {
root.postorder(limit)
}
}
#[test]
fn test() {
let root = tree!(
1,
tree!(
2,
tree!(4, tree!(8), tree!(9)),
tree!(-99, tree!(-99), tree!(-99))
),
tree!(
3,
tree!(-99, tree!(12), tree!(13)),
tree!(7, tree!(-99), tree!(14))
)
);
let limit = 1;
let res = tree!(
1,
tree!(2, tree!(4, tree!(8), tree!(9)), None),
tree!(3, None, tree!(7, None, tree!(14)))
);
assert_eq!(Solution::sufficient_subset(root, limit), res);
let root = tree!(
5,
tree!(4, tree!(11, tree!(7), tree!(1)), None),
tree!(8, tree!(17), tree!(4, tree!(5), tree!(3)))
);
let limit = 22;
let res = tree!(
5,
tree!(4, tree!(11, tree!(7), None), None),
tree!(8, tree!(17), tree!(4, tree!(5), None))
);
assert_eq!(Solution::sufficient_subset(root, limit), res);
let root = tree!(1, tree!(2, tree!(-5), None), tree!(-3, tree!(4), None));
let limit = -1;
let res = tree!(1, None, tree!(-3, tree!(4), None));
assert_eq!(Solution::sufficient_subset(root, limit), res);
}
// Accepted solution for LeetCode #1080: Insufficient Nodes in Root to Leaf Paths
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function sufficientSubset(root: TreeNode | null, limit: number): TreeNode | null {
if (root === null) {
return null;
}
limit -= root.val;
if (root.left === null && root.right === null) {
return limit > 0 ? null : root;
}
root.left = sufficientSubset(root.left, limit);
root.right = sufficientSubset(root.right, limit);
return root.left === null && root.right === null ? null : root;
}
Use this to step through a reusable interview workflow for this problem.
BFS with a queue visits every node exactly once — O(n) time. The queue may hold an entire level of the tree, which for a complete binary tree is up to n/2 nodes = O(n) space. This is optimal in time but costly in space for wide trees.
Every node is visited exactly once, giving O(n) time. Space depends on tree shape: O(h) for recursive DFS (stack depth = height h), or O(w) for BFS (queue width = widest level). For balanced trees h = log n; for skewed trees h = n.
Review these before coding to avoid predictable interview regressions.
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.