Skip to content

Commit 3d8a2fc

Browse files
committed
一刷1325
1 parent 0e65c9a commit 3d8a2fc

File tree

8 files changed

+93
-57
lines changed

8 files changed

+93
-57
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9300,14 +9300,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
93009300
//|{doc_base_url}/1324-print-words-vertically.adoc[题解]
93019301
//|Medium
93029302
//|
9303-
//
9304-
//|{counter:codes}
9305-
//|{leetcode_base_url}/delete-leaves-with-a-given-value/[1325. Delete Leaves With a Given Value^]
9306-
//|{source_base_url}/_1325_DeleteLeavesWithAGivenValue.java[Java]
9307-
//|{doc_base_url}/1325-delete-leaves-with-a-given-value.adoc[题解]
9308-
//|Medium
9309-
//|
9310-
//
9303+
9304+
|{counter:codes}
9305+
|{leetcode_base_url}/delete-leaves-with-a-given-value/[1325. Delete Leaves With a Given Value^]
9306+
|{source_base_url}/_1325_DeleteLeavesWithAGivenValue.java[Java]
9307+
|{doc_base_url}/1325-delete-leaves-with-a-given-value.adoc[题解]
9308+
|Medium
9309+
|
9310+
93119311
//|{counter:codes}
93129312
//|{leetcode_base_url}/minimum-number-of-taps-to-open-to-water-a-garden/[1326. Minimum Number of Taps to Open to Water a Garden^]
93139313
//|{source_base_url}/_1326_MinimumNumberOfTapsToOpenToWaterAGarden.java[Java]
Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,80 @@
11
[#1325-delete-leaves-with-a-given-value]
2-
= 1325. Delete Leaves With a Given Value
2+
= 1325. 删除给定值的叶子节点
33

4-
{leetcode}/problems/delete-leaves-with-a-given-value/[LeetCode - Delete Leaves With a Given Value^]
4+
https://leetcode.cn/problems/delete-leaves-with-a-given-value/[LeetCode - 1325. 删除给定值的叶子节点 ^]
55

6-
Given a binary tree `root` and an integer `target`, delete all the *leaf nodes* with value `target`.
6+
给你一棵以 `root` 为根的二叉树和一个整数 `target` ,请你删除所有值为 `target`*叶子节点*
77

8-
Note that once you delete a leaf node with value `target`*, *if it's parent node becomes a leaf node and has the value `<font face="monospace">target`, it should also be deleted (you need to continue doing that until you can't).
8+
注意,一旦删除值为 `target` 的叶子节点,它的父节点就可能变成叶子节点;如果新叶子节点的值恰好也是 `target`,那么这个节点也应该被删除。
99

10-
11-
*Example 1:*
10+
也就是说,你需要重复此过程直到不能继续删除。
1211

13-
image::https://assets.leetcode.com/uploads/2020/01/09/sample_1_1684.png[{image_attr}]
12+
*示例 1:*
1413

15-
[subs="verbatim,quotes,macros"]
16-
----
17-
*Input:* root = [1,2,3,2,null,2,4], target = 2
18-
*Output:* [1,null,3,null,4]
19-
*Explanation:* Leaf nodes in green with value (target = 2) are removed (Picture in left).
20-
After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
21-
----
14+
image::images/1325-01.png[{image_attr}]
2215

23-
*Example 2:*
16+
....
17+
输入:root = [1,2,3,2,null,2,4], target = 2
18+
输出:[1,null,3,null,4]
19+
解释:
20+
上面左边的图中,绿色节点为叶子节点,且它们的值与 target 相同(同为 2 ),它们会被删除,得到中间的图。
21+
有一个新的节点变成了叶子节点且它的值与 target 相同,所以将再次进行删除,从而得到最右边的图。
22+
....
2423

25-
image::https://assets.leetcode.com/uploads/2020/01/09/sample_2_1684.png[{image_attr}]
24+
*示例 2:*
2625

27-
[subs="verbatim,quotes,macros"]
28-
----
29-
*Input:* root = [1,3,3,3,2], target = 3
30-
*Output:* [1,3,null,null,2]
31-
----
26+
image::images/1325-02.png[{image_attr}]
3227

33-
*Example 3:*
28+
....
29+
输入:root = [1,3,3,3,2], target = 3
30+
输出:[1,3,null,null,2]
31+
....
3432

35-
image::https://assets.leetcode.com/uploads/2020/01/15/sample_3_1684.png[{image_attr}]
33+
*示例 3:*
3634

37-
[subs="verbatim,quotes,macros"]
38-
----
39-
*Input:* root = [1,2,null,2,null,2], target = 2
40-
*Output:* [1]
41-
*Explanation:* Leaf nodes in green with value (target = 2) are removed at each step.
42-
----
35+
image::images/1325-03.png[{image_attr}]
4336

44-
*Example 4:*
37+
....
38+
输入:root = [1,2,null,2,null,2], target = 2
39+
输出:[1]
40+
解释:每一步都删除一个绿色的叶子节点(值为 2)。
41+
....
4542

46-
[subs="verbatim,quotes,macros"]
47-
----
48-
*Input:* root = [1,1,1], target = 1
49-
*Output:* []
50-
----
5143

52-
*Example 5:*
53-
54-
[subs="verbatim,quotes,macros"]
55-
----
56-
*Input:* root = [1,2,3], target = 1
57-
*Output:* [1,2,3]
58-
----
59-
60-
61-
*Constraints:*
44+
*提示:*
6245

46+
* 树中节点数量的范围是 `[1, 3000]`
47+
* `+1 <= Node.val, target <= 1000+`
6348
64-
* `1 <= target <= 1000`
65-
* Each tree has at most `3000` nodes.
66-
* Each node's value is between `[1, 1000]`.
6749
50+
== 思路分析
6851

52+
深度优先遍历,而且做“后序”处理:在获取返回值后,对当前节点再做判断,这样就可以链式地删除掉符合条件的节点。
6953

7054
[[src-1325]]
55+
[tabs]
56+
====
57+
一刷::
58+
+
59+
--
7160
[{java_src_attr}]
7261
----
7362
include::{sourcedir}/_1325_DeleteLeavesWithAGivenValue.java[tag=answer]
7463
----
64+
--
65+
66+
// 二刷::
67+
// +
68+
// --
69+
// [{java_src_attr}]
70+
// ----
71+
// include::{sourcedir}/_1325_DeleteLeavesWithAGivenValue_2.java[tag=answer]
72+
// ----
73+
// --
74+
====
75+
76+
77+
== 参考资料
7578

79+
. https://leetcode.cn/problems/delete-leaves-with-a-given-value/solutions/101264/shan-chu-gei-ding-zhi-de-xie-zi-jie-dian-by-leet-2/[1325. 删除给定值的叶子节点 - 官方题解^]
80+
. https://leetcode.cn/problems/delete-leaves-with-a-given-value/solutions/423926/java-shu-de-hou-xu-bian-li-xiang-guan-wen-ti-by-sa/[1325. 删除给定值的叶子节点 - Java 树的后续遍历相关问题^]

docs/images/1325-01.png

23.6 KB
Loading

docs/images/1325-02.png

15 KB
Loading

docs/images/1325-03.png

13.9 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,7 @@ include::1289-minimum-falling-path-sum-ii.adoc[leveloffset=+1]
27332733

27342734
// include::1324-print-words-vertically.adoc[leveloffset=+1]
27352735

2736-
// include::1325-delete-leaves-with-a-given-value.adoc[leveloffset=+1]
2736+
include::1325-delete-leaves-with-a-given-value.adoc[leveloffset=+1]
27372737

27382738
// include::1326-minimum-number-of-taps-to-open-to-water-a-garden.adoc[leveloffset=+1]
27392739

logbook/202503.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,13 @@ endif::[]
788788
|{doc_base_url}/1561-maximum-number-of-coins-you-can-get.adoc[题解]
789789
|✅ 贪心。每次去第二大的数字即可。
790790

791+
792+
|{counter:codes2503}
793+
|{leetcode_base_url}/delete-leaves-with-a-given-value/[1325. 删除给定值的叶子节点^]
794+
|{doc_base_url}/1325-delete-leaves-with-a-given-value.adoc[题解]
795+
|✅ 深度优先遍历,在“后序”中做业务处理。
796+
797+
791798
|===
792799

793800
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
public class _1325_DeleteLeavesWithAGivenValue {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-05-22 23:48:19
11+
*/
12+
public TreeNode removeLeafNodes(TreeNode root, int target) {
13+
if (root == null) {
14+
return null;
15+
}
16+
root.left = removeLeafNodes(root.left, target);
17+
root.right = removeLeafNodes(root.right, target);
18+
if (root.left == null && root.right == null && root.val == target) {
19+
return null;
20+
}
21+
return root;
22+
}
23+
// end::answer[]
24+
}

0 commit comments

Comments
 (0)