《录鼎记》——二叉树

今日内容

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

一、二叉搜索树的最近公共祖先

力扣题目链接 (opens new window)

由于二叉搜索树是有序的,所以其需要找的公共祖先,一定在[p,q]之间

并且,从上向下遍历,第一次遇到cur节点的数值在区间内内,则cur就是怕,q的公共祖先。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

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

感觉确实简单一点。大体思路是如果均大于根节点,则往右子树遍历,若均小于根节点则往左子树遍历,依据是上方以确定的首次在[p,q]区间内即为结果。

迭代法

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

确实有被迭代法的简单惊叹到。

二、二叉搜索树中的插入操作

力扣题目链接 (opens new window)

写了半天,感觉自己写的没问题但过不了,不管怎么样,还是感觉太复杂了。

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* 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->right,val);
        if(cur->val>val) traversal(cur->left,val);
        return;
    }
    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) {
        if(root==NULL){
            TreeNode* node =new TreeNode(val);
            return node;
        }
        TreeNode* cur =root;
        TreeNode* parent =root;
        while(cur!=NULL){
            parent=cur;
            if(cur->val>val) cur=cur->left;
            else{
                cur=cur->right;
            }
        }
        TreeNode* node =new TreeNode(val);
        if(val<parent->val) parent->left =node;
        else parent->right =node;
        return root;

    }
};

迭代法的大致思路是:先找到对应的空节点,然后看其与父节点的关系确定放在左子树还是右子树当中,当然如果头节点为空则是直接作为根节点。

三、删除二叉搜索树中的节点

力扣题目链接 (opens new window)

一看挺复杂心凉半截(狗头)

递归三部曲:

1、确定递归函数参数及返回值

本题需要通过递归返回值来删除节点

TreeNode* deleteNode(TreeNode* root, int key)

2、确定终止条件

遇到空值则返回,因为没有找到删除的节点

if (root == nullptr) return root;

3、确定单层递归逻辑

可能情况一:没找到需要删除的节点,所以返回

可能情况二:此节点为叶子节点,直接删除没返回NULL

可能情况三:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子。

可能情况四:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子

可能情况五:左右孩子节点均不为空,则将删除节点左子树头节点放到删除节点右子树最左节点的做孩子上,并返回删除节点右孩子。

完整代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root==NULL) return root;
        if(root->val==key){
            if(root->left==NULL&&root->right==NULL){
                delete root;
                return nullptr;
            }
             else if(root->left==NULL){
            auto reNode =root->right;
            delete root;
            return reNode;
           }
        else if(root->right==nullptr){
            auto reNode =root->left
;
            delete root;
            return reNode;
            }
        else{
            TreeNode* cur =root->right;
            while(cur->left!=nullptr){
                cur=cur->left;
            }
            cur->left=root->left;
            TreeNode* tmp =root;
            root =root->right;
            delete tmp;
            return 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 root;
        if (root->val == key) {
            if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用
                return root->left;
            }
            TreeNode *cur = root->right;
            while (cur->left) {
                cur = cur->left;
            }
            swap(root->val, cur->val); // 这里第一次操作目标值:交换目标值其右子树最左面节点。
        }
        root->left = deleteNode(root->left, key);
        root->right = deleteNode(root->right, key);
        return root;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值