题目:235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点
参考链接:代码随想录
235. 二叉搜索树的最近公共祖先
思路:一个星期没做题稍微生疏了点,先回顾一下上题普通二叉树的最近公共祖先,方法是后序遍历,将左右孩子的结果逐渐往上传递,在到达头结点的时候返回最终结果。使用上题的方法也能完成这题。但我们尽可能需要用到BST的特性。对BST而言,如果某中间节点是p和q的公共祖先,则该节点的值必定位于[p,q]之间,如果中间节点cur的值大于p和q,则p和q的公共祖先必定都在中间节点的左子树上。故直接从中间节点二分查找即可,不需要遍历整个树,也不需要递归。时间复杂度O(logn)。
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode *cur=root;
if(p->val>q->val){
swap(p,q);//确保p的值小于q的值
}
while(cur&&!(cur->val>=p->val && cur->val<=q->val)){//只要cur的值不在中间,这里一定要有等号
if(cur->val<p->val){
cur=cur->right;
}
else{
cur=cur->left;
}
}
return cur;
}
};
看了标答之后发现还可以使用递归法:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root->val<p->val&&root->val<q->val){
return lowestCommonAncestor(root->right,p,q);
}
else if(root->val>p->val&&root->val>q->val){
return lowestCommonAncestor(root->left,p,q);
}
else{
return root;
}
}
};
701.二叉搜索树中的插入操作
思路:对BST,由于其有序性,我们可以优先考虑迭代法,即从根向左右分别搜索。本题比较容易,直接从头往下搜索,找到空的位置插入即可。时间复杂度O(logn)。
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
TreeNode* node=new TreeNode(val);
if(!root){
return node;
}
TreeNode* cur=root;
TreeNode* pre;
while(cur){
pre=cur;//记录前置节点
if(val>cur->val){
cur=cur->right;
}
else{
cur=cur->left;
}
}
if(val>pre->val){
pre->right=node;
}
else{
pre->left=node;
}
return root;
}
};
看了标答发现还可以递归,不过BST肯定不用递归整棵树。
标答:
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == NULL) {
TreeNode* node = new TreeNode(val);
return node;
}
if (root->val > val) root->left = insertIntoBST(root->left, val);
if (root->val < val) root->right = insertIntoBST(root->right, val);
return root;
}
};
450.删除二叉搜索树中的节点
思路:优先考虑迭代,删除节点需要调整BST的结构。我们分几种情况讨论,首先是要删除的节点就是叶子节点,这时候直接删掉即可;然后是要删除的节点左右孩子有一个为空,这时候只需要将其一个孩子提上来即可;**最后是要删除的节点cur的左右孩子均存在,这里我们仅考虑上提右孩子替代删除节点,然后考虑cur右孩子的左子树,如果存在,则需要将其挂到cur左孩子的最右边。**这个过程可以借助画图来理解。还要单独考虑cur为根节点的情况,在我们的代码实现中引入了两个bool值flag和flag2,前者用于记录cur是前置节点pre的左孩子还是右孩子,后者用于记录cur是否为根节点。时间复杂度O(logn)。
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
TreeNode* cur=root;
TreeNode* pre;
int flag;
while(cur&&key!=cur->val){
pre=cur;
if(key>cur->val){
cur=cur->right;
flag=1;
}
else{
cur=cur->left;
flag=0;
}
}
//flag为0表示cur在pre的左边,为1表示cur在pre的右边
if(!cur){//没找到,这里也包含了root为空的情况
return root;
}
bool flag2=(cur==root);//记录要删除的节点是不是根节点
if(!cur->left&&!cur->right){//cur为叶子节点
if(flag2){
root=nullptr;
}
else{
if(flag){
pre->right=nullptr;
}
else{
pre->left=nullptr;
}
}
delete cur;
}
else if(cur->left&&cur->right){//两边都有孩子,最复杂的情况
if(cur->right->left){//右孩子的左孩子存在,将其挂到左孩子的最右侧
TreeNode* cur2=cur->left;
TreeNode* pre2;
while(cur2){//寻找左孩子的最右侧
pre2=cur2;
cur2=cur2->right;
}
pre2->right=cur->right->left;
}
cur->right->left=cur->left;
if(flag2){//删除的是根
root=cur->right;
}
else{
if(flag){
pre->right=cur->right;
}
else{
pre->left=cur->right;
}
}
delete cur;
}
else{//只有一边有孩子
TreeNode* child=cur->left ? cur->left : cur->right;//记录唯一的非空孩子
if(flag2){
root=child;
}
else{
if(flag){
pre->right=child;
}
else{
pre->left=child;
}
}
delete cur;
}
return root;
}
};
可以看到本题的迭代法比较复杂,主要情况比较多,不好处理,看完标答将单独删除一个节点抽象成一个函数,减少了代码量,没有使用flag,而是直接判断值来确定是左右孩子。标答是将左节点提上去,我们写的是提右节点。
标答:
class Solution {
private:
// 将目标节点(删除节点)的左子树放到 目标节点的右子树的最左面节点的左孩子位置上
// 并返回目标节点右孩子为新的根节点
// 是动画里模拟的过程
TreeNode* deleteOneNode(TreeNode* target) {
if (target == nullptr) return target;
if (target->right == nullptr) return target->left;
TreeNode* cur = target->right;
while (cur->left) {
cur = cur->left;
}
cur->left = target->left;
return target->right;
}
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) return root;
TreeNode* cur = root;
TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur
while (cur) {
if (cur->val == key) break;
pre = cur;
if (cur->val > key) cur = cur->left;
else cur = cur->right;
}
if (pre == nullptr) { // 如果搜索树只有头结点
return deleteOneNode(cur);
}
// pre 要知道是删左孩子还是右孩子
if (pre->left && pre->left->val == key) {
pre->left = deleteOneNode(cur);
}
if (pre->right && pre->right->val == key) {
pre->right = deleteOneNode(cur);
}
return root;
}
};
递归法,思路类似:
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(!root){//没找到
return nullptr;
}
if(root->val==key){//主要删除找到值的时候
if(!root->left&&!root->right){//两边都为空
delete root;
return nullptr;
}
else if(root->left&&!root->right){
TreeNode* node=root->left;
delete root;
return node;
}
else if(!root->left&&root->right){
TreeNode* node=root->right;
delete root;
return node;
}
else{//两边都不为空
TreeNode* node=root->right;
TreeNode* cur=root->left;
while(cur->right){
cur=cur->right;
}
cur->right=node->left;
node->left=root->left;
delete root;
return node;
}
}
else if(root->val<key){
root->right=deleteNode(root->right,key);//去右子树删
}
else{
root->left=deleteNode(root->left,key);
}
return root;
}
};