Delete Leaves With a Given Value
给一个二叉树和一个target, 删除所有leaf的值为target.
先找到leaf, 然后再看target
/**
* Definition for a binary tree node.
* 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;
* }
* }
*/
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.left == null && root.right == null && root.val == target)
root = null;
return root;
}
}