> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/trees/insufficient-nodes-in-root-to-leaf-paths.md).

# Insufficient Nodes in Root to Leaf Paths

Given the `root` of a binary tree, consider all *root to leaf paths*: paths from the root to any leaf.  (A leaf is a node with no children.)

A `node` is *insufficient* if **every** such root to leaf path intersecting this `node` has sum strictly less than `limit`.

Delete all insufficient nodes simultaneously, and return the root of the resulting binary tree.

**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]
```

**Note:**

1. The given tree will have between `1` and `5000` nodes.
2. `-10^5 <= node.val <= 10^5`
3. `-10^9 <= limit <= 10^9`

```java
class Solution {
    public TreeNode sufficientSubset(TreeNode root, int limit) {
        if (root == null)
            return root;
        boolean ans = helper(root, limit, 0);
        if (ans == true)
            return null;
        return root;
    }

    public boolean helper(TreeNode root, int limit, int sum) {
        if (root.left == null && root.right == null) {
            sum += root.val;
            if (sum < limit)
                return true;
            return false;
        }
        if (root.left != null) {
            boolean ans = helper(root.left, limit, sum + root.val);
            if (ans)
                root.left = null;
        }
        if (root.right != null) {
            boolean ans = helper(root.right, limit, sum + root.val);
            if (ans)
                root.right = null;
        }
        if (root.left == null && root.right == null)
            return true;
        return false;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/trees/insufficient-nodes-in-root-to-leaf-paths.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
