Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ae30f6f

Browse files
add q1325
1 parent 1ae42b2 commit ae30f6f

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
- [q104_二叉树的最大深度](/src/递归/q104_二叉树的最大深度)
8585
- [q226_翻转二叉树](/src/递归/q226_翻转二叉树)
8686
- [q236_二叉树的最近公共祖先](/src/递归/q236_二叉树的最近公共祖先)
87+
- [q1325_删除给定值的叶子节点](/src/递归/q1325_删除给定值的叶子节点)
8788

8889
### 分治法/二分法
8990

‎README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
- [Question 104 : Maximum Depth of Binary Tree](/src/递归/q104_二叉树的最大深度)
8585
- [Question 226 : Invert Binary Tree](/src/递归/q226_翻转二叉树)
8686
- [Question 236 : Lowest Common Ancestor of a Binary Tree](/src/递归/q236_二叉树的最近公共祖先)
87+
- [Question 1325 : Delete Leaves With a Given Value](/src/递归/q1325_删除给定值的叶子节点)
8788

8889
### Divide and Conquer / Dichotomy
8990

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package 递归.q1325_删除给定值的叶子节点;
2+
3+
/**
4+
* 递归 o(n)
5+
*/
6+
public class Solution {
7+
8+
public TreeNode removeLeafNodes(TreeNode root, int target) {
9+
if (root == null) {
10+
return null;
11+
}
12+
13+
root.left = removeLeafNodes(root.left, target);
14+
root.right = removeLeafNodes(root.right, target);
15+
16+
if (root.val == target && root.left == null && root.right == null) {
17+
return null;
18+
}
19+
return root;
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package 递归.q1325_删除给定值的叶子节点;
2+
3+
public class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
8+
TreeNode() {
9+
}
10+
11+
TreeNode(int val) {
12+
this.val = val;
13+
}
14+
15+
TreeNode(int val, TreeNode left, TreeNode right) {
16+
this.val = val;
17+
this.left = left;
18+
this.right = right;
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /