|
| 1 | +<h2>1325. Delete Leaves With a Given Value</h2><h3>Medium</h3><hr><div><p>Given a binary tree <code>root</code> and an integer <code>target</code>, delete all the <strong>leaf nodes</strong> with value <code>target</code>.</p> |
| 2 | + |
| 3 | +<p>Note that once you delete a leaf node with value <code>target</code><strong>, </strong>if it's parent node becomes a leaf node and has the value <code><font face="monospace">target</font></code>, it should also be deleted (you need to continue doing that until you can't).</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong>Example 1:</strong></p> |
| 7 | + |
| 8 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/01/09/sample_1_1684.png" style="width: 550px; height: 120px;"></strong></p> |
| 9 | + |
| 10 | +<pre><strong>Input:</strong> root = [1,2,3,2,null,2,4], target = 2 |
| 11 | +<strong>Output:</strong> [1,null,3,null,4] |
| 12 | +<strong>Explanation:</strong> Leaf nodes in green with value (target = 2) are removed (Picture in left). |
| 13 | +After removing, new nodes become leaf nodes with value (target = 2) (Picture in center). |
| 14 | +</pre> |
| 15 | + |
| 16 | +<p><strong>Example 2:</strong></p> |
| 17 | + |
| 18 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/01/09/sample_2_1684.png" style="width: 300px; height: 120px;"></strong></p> |
| 19 | + |
| 20 | +<pre><strong>Input:</strong> root = [1,3,3,3,2], target = 3 |
| 21 | +<strong>Output:</strong> [1,3,null,null,2] |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong>Example 3:</strong></p> |
| 25 | + |
| 26 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/01/15/sample_3_1684.png" style="width: 420px; height: 150px;"></strong></p> |
| 27 | + |
| 28 | +<pre><strong>Input:</strong> root = [1,2,null,2,null,2], target = 2 |
| 29 | +<strong>Output:</strong> [1] |
| 30 | +<strong>Explanation:</strong> Leaf nodes in green with value (target = 2) are removed at each step. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong>Example 4:</strong></p> |
| 34 | + |
| 35 | +<pre><strong>Input:</strong> root = [1,1,1], target = 1 |
| 36 | +<strong>Output:</strong> [] |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p><strong>Example 5:</strong></p> |
| 40 | + |
| 41 | +<pre><strong>Input:</strong> root = [1,2,3], target = 1 |
| 42 | +<strong>Output:</strong> [1,2,3] |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= target <= 1000</code></li> |
| 50 | + <li>The given binary tree will have between <code>1</code> and <code>3000</code> nodes.</li> |
| 51 | + <li>Each node's value is between <code>[1, 1000]</code>.</li> |
| 52 | +</ul> |
| 53 | +</div> |
0 commit comments