递归~~~
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return null;
if(root.val > key){
root.left = deleteNode(root.left, key);
return root;
}
if(root.val < key){
root.right = deleteNode(root.right, key);
return root;
}
if(root.val == key){
if(root.left == null && root.right == null){
return null;
}
if(root.left == null) return root.right;
if(root.right == null) return root.left;
TreeNode tmp = root.right;
while(tmp.left != null){
tmp = tmp.left;
}
root.right = deleteNode(root.right, tmp.val);
tmp.right = root.right;
tmp.left = root.left;
return tmp;
}
return root;
}
}