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;
}
}
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;
}
}
q1325_删除给定值的叶子节点
作品《LeetCode题目分类与面试问题整理 - q1325_删除给定值的叶子节点》由 不喝星巴克 发布于 匠果,转载请注明出处及链接地址:
http://www.jiangguo.net/c/9r6/l22.html