力扣算法学习day17-3
450-删除二叉搜索树中的结点
题目
代码实现
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null){
return null;
}
if(key == root.val){
if(root.left == null && root.right == null){
return null;
}
if(root.left == null){
return root.right;
}
if(root.right == null){
return root.left;
}
TreeNode temp = root.right;
while(temp.left != null){
temp = temp.left;
}
temp.left = root.left;
return root.right;
}
if(key < root.val){
root.left = deleteNode(root.left,key);
}else{
root.right = deleteNode(root.right,key);
}
return root;
}
}
|