算法训练第二十二天|235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点

二叉树|235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点


235. 二叉搜索树的最近公共祖先

自己审题思路

和普通二叉树寻找最近公共祖先一样,看到搜索树想着可以利用二叉搜索树特性,但是一时没想到怎么用。

看完代码随想录题解后的收获

1、 利用二叉搜索树特性,持续判断向左向右遍历。
2、公共递归什么时候有返回值(遍历一条边、遍历整棵树且需要处理递归返回值
3、搜索一条边和搜索整棵树递归写法

代码(递归):
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == nullptr) return nullptr;

        // if(root->val > p->val && root->val < q->val) return root;
        if(root->val > p->val && root->val > q->val) {
            return lowestCommonAncestor(root->left, p, q);
        } else if (root->val < p->val && root->val < q->val) {
            return lowestCommonAncestor(root->right, p, q);
        } else return root;
        
    }
};
代码(迭代):
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == nullptr) return nullptr;
        while(root) {//持续向左向右遍历
            if(root->val > p->val && root->val > q->val) {
                root = root->left;
            } else if (root->val < p->val && root->val < q->val) {
                root = root->right;
            } else return root;
        }
        
        return nullptr;
    }
};

参考详解


701.二叉搜索树中的插入操作

自己审题思路

根据二叉搜索树特性遍历二叉树,然后在叶子节点添加元素,并不改变树的结构。

看完代码随想录题解后的收获

递归法有无返回值处理情况

代码(递归有返回值):
class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        
        if(root == nullptr) {
            TreeNode* newNode = new TreeNode(val);
            return newNode;
        }

        if(val < root->val){
            root->left = insertIntoBST(root->left, val);
        } else if (val > root->val) {
            root->right = insertIntoBST(root->right, val);
        }

        return root;
    }
};
代码(递归无返回值):
class Solution {
private:
    TreeNode* parent;
    void traversal(TreeNode* cur, int val) {
        if (cur == NULL) {
            TreeNode* node = new TreeNode(val);
            if (val > parent->val) parent->right = node;
            else parent->left = node;
            return;
        }
        parent = cur;
        if (cur->val > val) traversal(cur->left, val);
        if (cur->val < val) traversal(cur->right, val);
        return;
    }

public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        parent = new TreeNode(0);
        if (root == NULL) {
            root = new TreeNode(val);
        }
        traversal(root, val);
        return root;
    }
};
代码(迭代):
class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        TreeNode* newNode = new TreeNode(val);
        if(root == nullptr) return newNode;

        TreeNode* cur = root;
        TreeNode* pre = nullptr; // 记录前一个节点
        int direction;// 记录向左遍历还是向右遍历
        while(cur) {
            if(val < cur->val) {
                pre = cur;
                cur = cur->left;
                direction = 0;
            } else if(val > cur->val) {
                pre = cur;
                cur = cur->right;
                direction = 1;
            }
        }
        if(direction == 0) {
            pre->left = newNode;
        } else {
            pre->right = newNode;
        }
        return root;
    }
};

参考详解


450.删除二叉搜索树中的节点

自己审题思路

删除二叉树需要重构二叉树,比较复杂,第一次看没有思路。

看完代码随想录题解后的收获

删除节点后不同情况,改变树的结构。

代码:
class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (root == nullptr) return root; //1.没找到,不删除

        if (root->val == key) {
            if (root->left == nullptr && root->right == nullptr) {//2.叶子节点(左右孩子都为空)
                delete root;
                return nullptr;
            }
            else if (root->left == nullptr) {//3.左空右不空
                auto Node = root->right;
                delete root;
                return Node;
            }
            else if (root->right == nullptr) {//4.右空左不空
                auto Node = root->left;
                delete root;
                return Node;
            }
            else {//5.左右都不空,将左孩子继承到右孩子最左边
                TreeNode* cur = root->right;
                while (cur->left != nullptr){//找到右孩子最左边的叶子节点
                    cur = cur->left;
                }
                cur->left = root->left;//将左孩子继承到右孩子最左边
                TreeNode* Node = root->right; 
                delete root;
                return Node;// 返回旧root的右孩子作为新root
            }

        }

        if (root->val > key) root->left = deleteNode(root->left, key);
        if (root->val < key) root->right = deleteNode(root->right, key);
        return root;
    }
};
代码(左右都不空,将右孩子继承到左孩子最右边)
class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root == nullptr) return nullptr;
        if(root->val == key) {
            if(!root->left && !root->right) return nullptr;
            else if(root->left && !root->right) return root->left;
            else if(!root->left && root->right) return root->right;
            else {
                TreeNode* cur = root->left;
                TreeNode* pre = nullptr;
                while(cur) {
                    pre = cur;
                    cur = cur->right;
                }
                pre->right = root->right;
                return root->left;
            }
        }

        root->left = deleteNode(root->left, key);
        root->right= deleteNode(root->right, key);
        return root;
    }
};

1、上述代码没有删除节点逻辑;
2、上述代码没有利用二搜索树的特性来减少递归次数。
这里的if(root->left)if(root->right)可以省略是因为:我们要处理的节点查找是通过root->val == key,不加判断我们也可以通过常规全局遍历找到,上述判断if(root->left)if(root->right)只是减少了递归次数。
235. 二叉搜索树的最近公共祖先701.二叉搜索树中的插入操作两题都不能省略,因为上述两道题都通过二叉搜索树的特性寻找待处理节点,没有判断就寻找不到待处理节点。

参考详解


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容