parent
1ae42b25c1
commit
ae30f6fa6c
@ -0,0 +1,21 @@
|
|||||||
|
package 递归.q1325_删除给定值的叶子节点;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归 o(n)
|
||||||
|
*/
|
||||||
|
public class Solution {
|
||||||
|
|
||||||
|
public TreeNode removeLeafNodes(TreeNode root, int target) {
|
||||||
|
if (root == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
root.left = removeLeafNodes(root.left, target);
|
||||||
|
root.right = removeLeafNodes(root.right, target);
|
||||||
|
|
||||||
|
if (root.val == target && root.left == null && root.right == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package 递归.q1325_删除给定值的叶子节点;
|
||||||
|
|
||||||
|
public class TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode left;
|
||||||
|
TreeNode right;
|
||||||
|
|
||||||
|
TreeNode() {
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode(int val) {
|
||||||
|
this.val = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode(int val, TreeNode left, TreeNode right) {
|
||||||
|
this.val = val;
|
||||||
|
this.left = left;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue