算法训练 Day 22 | 二叉树(8):235. 二叉搜索树的最近公共祖先,701.二叉搜索树中的插入操作,450.删除二叉搜索树中的节点

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

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

  •  第一想法:二叉搜索树的特性——遍历到的第一个范围在[p,q]内的结点为公共祖先
  • 看到题解想法:先想到递归法,题解有迭代法,即:使用一个循环,判断指针往左移动or往右移动
  • 遇到困难:无
  • Java:迭代法

     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            TreeNode cur = root;
            while (cur != null) {
                if (cur.val > p.val && cur.val > q.val) {
                    cur = cur.left;
                } else if (cur.val < p.val && cur.val < q.val) {
                    cur = cur.right;
                } else {
                    break;
                }
            }
            return cur;
        }

    Go:递归法

     func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    	if root == nil {
            return nil
        }
        // 找到第一个满足[p.val, q.Val]的值
        if p.Val <= q.Val && root.Val >= p.Val && root.Val <= q.Val {
            return root
        }
        if q.Val < p.Val && root.Val >= q.Val && root.Val <= p.Val {
            return root
        }
    
        left := lowestCommonAncestor(root.Left, p, q)
        right := lowestCommonAncestor(root.Right, p, q)
    
        if left != nil {
            return left
        } else {
            return right
        }
        return nil
    }
  • 总结与收获:二叉搜索树还可以使用迭代法,用一个循环解决问题

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

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

  •  第一想法:利用二叉搜索树的特性遍历到相应位置,然后插入
  • 看到题解想法:遍历+迭代法
  • 遇到困难:无

Java:递归法

 public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return new TreeNode(val);

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

Go:迭代法

func insertIntoBST(root *TreeNode, val int) *TreeNode {
    // 新增节点需要考虑构建插入的值
    // 创建go的结构体需要注意:1. 结构体前要加&取地址;2. 里面不完全值需要加逗号,
    if root == nil {
        return &TreeNode{
            Val: val,
        }
    }
    n := root
    for n != nil {
        // 1. 判断往左移动还是往右移动
        if n.Val < val {
            // 2. 判断移动还是新增
            if n.Right != nil {
                n = n.Right
            } else {
                n.Right = &TreeNode{
                    Val: val,
                }
                break
            }
        } else {
            if n.Left != nil {
                n = n.Left
            } else {
                n.Left = &TreeNode{
                    Val: val,
                }
                break
            }
           
        }
    }
    return root
}
  • 总结与收获:用时15min

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

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

  • 第一想法:明确注释中的4种情况
  • 看到题解想法:同上
  • 遇到困难:无

Java

 public TreeNode deleteNode(TreeNode root, int key) {
        // 情况0:找不到要删除的结点
        if (root == null) return null;

        if (root.val == key) {
            // 情况1:节点没有左右子树,直接删除(返回null)
            // 情况2:节点左子树为空,右子树不为空,返回右子树
            if (root.left == null) return root.right;
            // 情况3:节点左子树不为空,右子树为空,返回左子树
            if (root.right == null) return root.left;
            // 情况4:左右子树都不为空,则把左子树拼到右子树的最左侧,最后返回右子树
            TreeNode cur = root.right;
            while (cur.left != null) cur = cur.left;

            TreeNode rightTop = root.right;
            cur.left = root.left;
            return rightTop;
        }

        if (root.val > key) root.left = deleteNode(root.left, key);
        if (root.val < key) root.right = deleteNode(root.right, key);

        return root;
    }

Go

 func deleteNode(root *TreeNode, key int) *TreeNode {
    if root == nil {
        return nil
    }
    if root.Val == key {
        if root.Left == nil {
            return root.Right
        }
        if root.Right == nil {
            return root.Left
        }
        var n *TreeNode = root.Left
        for n.Right != nil {
            n = n.Right
        }
        n.Right = root.Right
        return root.Left
    }
    root.Left = deleteNode(root.Left, key)
    root.Right = deleteNode(root.Right, key)

    return root
}
  • 总结与收获:用时20min

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值