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

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

力扣

二叉树:公共祖先问题 (opens new window)中,如果递归函数有返回值,如何区分要搜索一条边,还是搜索整个树。

搜索一条边的写法:

if (递归函数(root->left)) return ;
if (递归函数(root->right)) return ;

搜索整个树写法:

left = 递归函数(root->left);
right = 递归函数(root->right);
left与right的逻辑处理;

本题就是标准的搜索一条边的写法,遇到递归函数的返回值,如果不为空,立刻返回。

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return root;
        }
        //root的值都大于p,q则在left中查找
        if(root.val > p.val && root.val > q.val) {
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            if(left != null) {
                return left;
            }
        }
        if(root.val < p.val && root.val < q.val) {
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if(right != null) {
                return right;
            }
        }
        return root;
        // if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
        // if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
        // return root;
    }
}

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

力扣

递归:1、找递归终止条件:找到遍历的节点为null的时候,就是要插入节点的位置了,并把插入的节点返回。

               这里把添加的节点返回给上一层,就完成了父子节点的赋值操作了

           2、每一级递归返回的信息是什么:返回构造好的节点

           3、一次递归做了什么:搜索树是有方向了,可以根据插入元素的数值,决定递归方向。

到这里,大家应该能感受到,如何通过递归函数返回值完成了新加入节点的父子关系赋值操作了,下一层将加入节点返回,本层用root->left或者root->right将其接住

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

迭代法:迭代法也很容易理解,和二分查找一样。记录父节点,最后比较父节点和val的值看看插在左边还是右边。 

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        //如果是空树,直接构建node节点
        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(parent.val > val) {
            parent.left = node;
        }else {
            parent.right = node;
        }
        return root;
    }
}

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

力扣

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        //递归出口
        if(root == null) {
            return root;
        }
        //单层递归处理逻辑
        if(root.val == key) {
            // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
            // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
            if(root.left == null) return root.right;
            // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
            else if(root.right == null) return root.left;
            // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
    // 并返回删除节点右孩子为新的根节点。
            else {
                TreeNode cur = root.right;
                while (cur.left != null) {//找到右子树的最左边节点
                    cur = cur.left;
                }
                cur.left = root.left;// 把要删除的节点(root)左子树放在cur的左孩子的位置
                root = root.right;// 返回旧root的右孩子作为新root
                //相当于某个节点替代了要删除的节点
                return root;
            }
        }
        //这里相当于把新的节点返回给上一层,上一层就要用 root->left 或者 root->right接住
        if (root.val > key) root.left = deleteNode(root.left, key);
        if (root.val < key) root.right = deleteNode(root.right, key);
        return root;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值