DAY22:二叉搜索树235、701、450

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

昨天学习了二叉树的公共祖先,今天是二叉搜索树的公共祖先,因为是二叉搜索树,所以有额外的性质可以利用来构建更高效的算法。

思路:判断根节点,如果比p\q都大,说明p\q在中间节点的左子树上,如果都小,就说明在中间节点的右子树上,如果在中间,说明就找到了最近的公共祖先。

递归法

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(p->val < root->val && q->val < root->val) return lowestCommonAncestor(root->left, p, q);
        else if(p->val > root->val && q->val > root->val) return lowestCommonAncestor(root->right, p, q);
        else return root;    
    }
};

迭代法

基本思路是一样的。

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        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 NULL;
    }
};

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

插入节点确实有多种插入方式,但是这题没有对插入后的树的深度进行限制,所以我们统一操作将插入的节点在叶子节点插入,降低困难。不需要重构树。

递归法

基本思路:

  • 终止条件:当搜索到叶子节点时候,将数值加到叶子节点上。
  • 逻辑:当数值比中间节点小的时候,往左子树移动搜索,如果大于就往右子树移动搜索,直到叶子节点。
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;
    }
};

迭代法

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if (root == NULL) {
            TreeNode* node = new TreeNode(val);
            return node;//对根节点为空的情况进行赋值
        }
        TreeNode* node = root;
        TreeNode* parent = root;//定义上一个父节点,用于链接节点
        while(node != NULL){//当没有遇到叶子节点的时候,就不断往下寻找叶子节点
            parent = node;
            if(node->val < val) node = node->right;
            else node = node->left;
        }
        TreeNode* cur = new TreeNode(val);//对新节点进行赋值
        if(val > parent->val) parent->right = cur;//将新的节点连接到父节点上
        else parent->left = cur;
        return root;//返回结果
    }
};

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

删除节点就要比增加节点更加困难了。因为在删除节点之后涉及到了树的重构。

因为总体思路是从上到下,因此是前序遍历。

5种情况

  • 没找到要删除的节点
  • 删除的节点为叶子节点
  • 删除的节点左子树不为空,右子树为空(直接将左子树接到删除节点的父节点上)
  • 删除的节点左子树为空,右子树不为空
  • 删除的节点左右子树都为空,所以涉及到了结构重构(如果选择右子树去继位的话,就直接将左子树放在右子树最左边的叶子节点左边,如果是左子树继位,就把右子树放在左子树最右边的叶子节点右边)

递归法

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root == NULL) return NULL;//如果没有要删除的点
        if(root->val == key){
            if(root->left == NULL && root->right == NULL) return NULL;//如果要删除的点就是叶子节点,直接删除
            else if(root->left != NULL && root->right == NULL) return root->left;//如果删除的点左子树不为空,返回左子树
            else if(root->left == NULL && root->right != NULL) return root->right;//如果删除的点右子树不为空,返回右子树
            else{
                //选择左子树上位
                TreeNode* node = root->left;//先找到左子树中最右边的叶子节点定位
                while(node->right != NULL){
                    node = node->right;
                }
                node->right = root->right;//将右子树放在左子树中最右边的定位点
                return root->left;//返回左子树
            }
        }
        if (root->val > key) root->left = deleteNode(root->left, key);//递归
        if (root->val < key) root->right = deleteNode(root->right, key);//采用前序遍历
        return root;
    }
};

如果想要更加优化内存的话,就需要及时delete释放掉要删除节点的内存。加上下述的代码来释放内存。

auto retNode = root->right;
///! 内存释放
delete root;
return retNode;

迭代法

比较复杂。下述代码就是代码随想录中的代码代码随想录

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;
    }
};

小结

作为二叉搜索树,因为其本身的性质,可以有优化的解法,实际上普通二叉树的通法也可以使用。对于二叉搜索树,还需要进一步总结归纳。

你可能感兴趣的:(leetcode刷题系列,算法,数据结构,leetcode,笔记,c++,学习)