> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Delete Leaves With a Given Value

> Tested Python solution for LeetCode 1325 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1325, Medium. Topics: Tree, Depth-First Search, Binary Tree. [View on LeetCode](https://leetcode.com/problems/delete-leaves-with-a-given-value/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1325   # by problem number
lcpy gen -s delete_leaves_with_a_given_value   # by problem name
```

## Problem

Given a binary tree `root` and an integer `target`, delete all the **leaf nodes** with value `target`.

Note that once you delete a leaf node with value `target`, if its parent node becomes a leaf node and has the value `target`, it should also be deleted (you need to continue doing that until you cannot).

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/01/09/sample_1_1684.png)

```
Input: root = [1,2,3,2,null,2,4], target = 2
Output: [1,null,3,null,4]
Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left).
After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
```

![Example 2](https://assets.leetcode.com/uploads/2020/01/09/sample_2_1684.png)

```
Input: root = [1,3,3,3,2], target = 3
Output: [1,3,null,null,2]
```

![Example 3](https://assets.leetcode.com/uploads/2020/01/15/sample_3_1684.png)

```
Input: root = [1,2,null,2,null,2], target = 2
Output: [1]
Explanation: Leaf nodes in green with value (target = 2) are removed at each step.
```

### Constraints

* The number of nodes in the tree is in the range `[1, 3000]`
* `1 <= Node.val, target <= 1000`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/delete_leaves_with_a_given_value/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/delete_leaves_with_a_given_value/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n) — visits each node once in post-order
    # Space: O(h) recursion stack, h = tree height
    def remove_leaf_nodes(self, root: TreeNode[int] | None, target: int) -> TreeNode[int] | None:
        if root is None:
            return None

        root.left = self.remove_leaf_nodes(root.left, target)
        root.right = self.remove_leaf_nodes(root.right, target)

        # Post-order: decide after children are pruned, so a node whose children
        # were just removed can itself qualify as a target leaf.
        if root.left is None and root.right is None and root.val == target:
            return None
        return root
```

## Complexity

| Time                                       | Space                                 |
| ------------------------------------------ | ------------------------------------- |
| O(n) — visits each node once in post-order | O(h) recursion stack, h = tree height |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
