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

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

题目链接:530. 二叉搜索树的最小绝对差 - 力扣(LeetCode)

文章讲解:代码随想录 (programmercarl.com)

 思路:二叉搜索树的最近公共祖先,从上到下遍历第一个val<= >=p,q的就是他们的公共祖先;

因为结点左子树上都小于他,右子树上都大于他;举例证明

递归:

    public TreeNode lowestCommon2(TreeNode node, TreeNode p,TreeNode q) {
        if(node == null || (node.val <= p.val && node.val >= q.val) || (node.val >= p.val && node.val <= q.val))return node;
        TreeNode left = lowestCommon2(node.left, p, q);
        TreeNode right = lowestCommon2(node.right, p, q);
        return left == null ? right : left;
    }

 从上到下遍历,前序遍历

carl哥的代码:

    public TreeNode lowestCommon3(TreeNode node, TreeNode p,TreeNode q) {
        if(node == null)return node;
        if(node.val > p.val && node.val > q.val) {
            TreeNode left = lowestCommon3(node.left, p, q);
            if(left != null)return left;
        }
        if(node.val < p.val && node.val < q.val) {
            TreeNode right = lowestCommon3(node.right, p, q);
            if(right != null)return right;
        }
        return node;
    }

更容易转成迭代:

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 
        while(root != null) {
            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;
        // return lowestCommon3(root,p,q);
    }

 

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

题目链接:501. 二叉搜索树中的众数 - 力扣(LeetCode)

文章讲解:代码随想录 (programmercarl.com)

思路:也是二叉搜索树的性质,观察发现不管要插入多大的值,都可以在null处插,

他包括了所有的值

递归的终止条件:如果为空就返回new TreeNode(val)

    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root == null)return new TreeNode(val);
        if(root.val > val) root.left = insertIntoBST(root.left,val);
        if(root.val < val) root.right = insertIntoBST(root.right,val);
        return root;
    }

迭代:todo 

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

题目链接:​​​​​​​. - 力扣(LeetCode)

文章讲解:代码随想录 (programmercarl.com)

 思路:

删除结点有5点要考虑:

  1. 如果没找到目标结点就返回null;
  2. 如果是叶子结点就返回null给上一层,上一层可能把他设置为左孩子或者右孩子
  3. 如果左孩子为空返回右孩子
  4. 如果右孩子为空,返回左孩子
  5. 如果都不为空,就把左孩子嫁接到右孩子的最左边,(考虑二叉搜索树的性质)
    public TreeNode delete(TreeNode node, int key) {
        if(node == null) return null;
        if(node.val == key) {
            if(node.left == null && node.right == null) {
                return null;
            }
            if(node.left == null)  return node.right;
            if(node.right == null) return node.left;

            TreeNode cur = node.right;
            while(cur.left != null) {
                cur = cur.left;
            }
            cur.left = node.left;
            return node.right;
        }
        node.left = delete(node.left,key);
        node.right = delete(node.right, key);
        return node;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值