算法刷题Day 22 二叉搜索树的最近公共祖先+二叉搜索树中的插入操作+删除二叉搜索树中的节点

Day 22 二叉树

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

根据二叉搜索树的性质,相比普通二叉树可以极大程度的简化代码,作为公共祖先其值一定在两个给定节点值之间,从树根往下遍历,第一次出现两个给定节点值之间的值,那个节点即为最近公共祖先(为什么是最近不是最远?根节点一般为最远,第一次出现的值处于两个给定节点值之间的节点为最近)

递归法

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

迭代法

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (p->val > q->val)
        {
            swap(p, q);
        }

        TreeNode *cur = root;
        while (cur)
        {
            if (cur->val < p->val)
            {
                cur = cur->right;
            }
            else if (cur->val > q->val)
            {
                cur = cur->left;
            }
            else
            {
                return cur;
            }
        }

        return nullptr;
    }
};

代码随想录里的解法更加简洁

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

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

没有要求平衡再简单不过,直接按搜索的方式遍历到最后一个叶子节点,在叶子节点下添加新的节点就可以了

迭代法

用迭代法简单明了

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        TreeNode *cur = root, *pre = nullptr;
        while (cur)
        {
            pre = cur;
            if (cur->val < val)
            {
                cur = cur->right;
            }
            else if (cur->val > val)
            {
                cur = cur->left;
            }
        }

        if (pre)
        {
            if (pre->val < val)
            {
                pre->right = new TreeNode(val);
            }
            else
            {
                pre->left = new TreeNode(val);
            }
            return root;
        }
        else return new TreeNode(val);
    }
};

递归法

记住要记录父节点,就有大概的思路了

class Solution {
    TreeNode *parent= nullptr;

    void traversal(TreeNode *root, int val)
    {
        if (!root)
        {
            TreeNode *node = new TreeNode(val);
            if (parent->val < val) parent->right = node;
            else parent->left = node;
            return;
        }
        parent = root;

        if (root->val < val) traversal(root->right, val);
        if (root->val > val) traversal(root->left, val);  
    }

public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if (!root) return new TreeNode(val);
        traversal(root, val);
        return root;      
    }
};

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

写麻了,涉及到结构调整,有点难

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (!root) return nullptr;
        else if (root->val < key)
        {
            root->right = deleteNode(root->right, key);
        }
        else if (root->val > key)
        {
            root->left = deleteNode(root->left, key);
        }
        else
        {
            if (!root->left && !root->right)
            {
                delete root;
                return nullptr;
            }
            else if (!root->left)
            {
                TreeNode *ret = root->right;
                delete root;
                return ret;
            }
            else if (!root->right)
            {
                TreeNode *ret = root->left;
                delete root;
                return ret;
            }
            else 
            {
                TreeNode *cur = root->right;
                while (cur->left)
                {
                    cur = cur->left;
                }
                cur->left = root->left;
                TreeNode *tmp = root;
                root = root->right;
                delete tmp;
                return root;
            }
        }

        return root;
    }
};

删除节点太难了,这里就不写迭代了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值