代码随想录算法训练营|day22

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

递归:如果p,q 位于root 的两侧,返回root;否则必然同大于root.Val同小于root.Val,用p.Val或q.Val来判断,第一次满足递归终止条件的即为父亲节点。

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
	if (root.Val - p.Val) * (root.Val - q.Val) <= 0 {
        return root
    }
    if root.Val < p.Val {
        return lowestCommonAncestor(root.Right, p, q)
    }else {
        return lowestCommonAncestor(root.Left, p, q)
    }
}

迭代

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    for root != nil {
        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 root
}

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

递归:找到val满足二叉搜索数性质的空节点位置直接插入。

func insertIntoBST(root *TreeNode, val int) *TreeNode {
    node := &TreeNode{Val:val}
    if root == nil {
        return node
    }
    if root.Val < val {
        root.Right = insertIntoBST(root.Right, val)
    }else if root.Val > val {
        root.Left = insertIntoBST(root.Left, val)
    }
    return root
}

迭代 :找到待插入节点的位置【parent】,与当前节点值比较插入

func insertIntoBST(root *TreeNode, val int) *TreeNode {
    node := &TreeNode{Val:val}
    if root == nil {
        return node
    }
    cur := root
    parent := cur
    for cur != nil {
        parent = cur
        if cur.Val > val {
            cur = cur.Left
        }else {
            cur = cur.Right
        }
    }
    if val > parent.Val {
        parent.Right = node
    }else{
        parent.Left = node
    }
    return root
}

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

递归:找到待删除节点,判断当前节点,若无左子树返回root.Right;若无右子树,返回root.Left,否则将当前节点的左子树添加到右子树最左节点【树的最小位置】下,返回root.Right。

func deleteNode(root *TreeNode, key int) *TreeNode {
    if root == nil {
        return root
    }
    if key > root.Val {
        root.Right = deleteNode(root.Right, key)
    }else if key < root.Val {
        root.Left = deleteNode(root.Left, key)
    }else{
        if root.Left == nil {
            return root.Right
        } 
        if root.Right == nil {
            return root.Left
        }
        node := root.Right
        for node.Left != nil {
            node = node.Left
        }
        node.Left = root.Left
        root = root.Right
    }
    return root
}

代码随想录文章详解

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

总结

二叉搜索树中的插入操作看题目要求以为得旋转树,去瞅瞅平衡二叉树旋转树那部分
对于二叉树的理解终于到了看山不是山的境界了

相关推荐

  1. 代码随想算法训练|day22

    2024-02-01 14:46:02       34 阅读
  2. 代码随想算法训练Day24|77. 组合

    2024-02-01 14:46:02       35 阅读
  3. 代码随想算法训练|day20

    2024-02-01 14:46:02       36 阅读
  4. 代码随想算法训练|day21

    2024-02-01 14:46:02       40 阅读
  5. 代码随想训练24day-贪心算法2

    2024-02-01 14:46:02       15 阅读
  6. 代码随想训练23day-贪心算法

    2024-02-01 14:46:02       13 阅读
  7. 代码随想训练26day-贪心算法4

    2024-02-01 14:46:02       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-01 14:46:02       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-01 14:46:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-01 14:46:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-01 14:46:02       18 阅读

热门阅读

  1. Android studio布局详解

    2024-02-01 14:46:02       34 阅读
  2. 达梦数据库存储过程

    2024-02-01 14:46:02       26 阅读
  3. vue3:中warch监听的几种写法

    2024-02-01 14:46:02       33 阅读
  4. ModuleNotFoundError: No module named ‘flask._compat‘

    2024-02-01 14:46:02       28 阅读
  5. rsync将远程文件同步到本地

    2024-02-01 14:46:02       30 阅读
  6. Docker加固策略,防止攻击

    2024-02-01 14:46:02       28 阅读
  7. Unity3D 如何获取动态生成的物体的数据详解

    2024-02-01 14:46:02       35 阅读