前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >LeetCode 701: 二叉搜索树中的插入操作 Insert into a Binary Search Tree

LeetCode 701: 二叉搜索树中的插入操作 Insert into a Binary Search Tree

作者头像
爱写bug
发布于 2020-03-25 08:54:23
发布于 2020-03-25 08:54:23
96100
代码可运行
举报
文章被收录于专栏:爱写Bug爱写Bug
运行总次数:0
代码可运行

题目:

给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。返回插入后二叉搜索树的根节点。保证原始二叉搜索树中不存在新值。

Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。你可以返回任意有效的结果。

Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.

例如,

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
给定二叉搜索树:

        4
       / \
      2   7
     / \
    1   3

和 插入的值: 5

你可以返回这个二叉搜索树:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
         4
       /   \
      2     7
     / \   /
    1   3 5

或者这个树也是有效的:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
         5
       /   \
      2     7
     / \   
    1   3
         \
          4

解题思路:

二叉搜索树的插入操作与搜索操作类似,对于每个节点:

  1. 根据节点值与目标节点值的关系,搜索左子树或右子树;
  2. 如果目标值小于节点的值,则继续在左子树中搜索;
  3. 如果目标值大于节点的值,则继续在右子树中搜索。
  4. 重复步骤 1 直到到达外部节点;
  5. 根据节点的值与目标节点的值的关系,将新节点添加为其左侧或右侧的子节点。

递归法:

Java:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null)
            return new TreeNode(val);
        if (val > root.val)
            root.right = insertIntoBST(root.right, val);
        else
            root.right = insertIntoBST(root.right, val);
        return root;
    }
}

Python:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return TreeNode(val)
        if val > root.val:
            root.right = self.insertIntoBST(root.right, val)
        else:
            root.left = self.insertIntoBST(root.left, val)
        return root

Golang:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func insertIntoBST(root *TreeNode, val int) *TreeNode {
    if root == nil {
        t := new(TreeNode)
        t.Val = val
        return t
    }
    if val > root.Val {
        root.Right = insertIntoBST(root.Right, val)
    } else {
        root.Left = insertIntoBST(root.Left, val)
    }
    return root
}

迭代法:

Java:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        TreeNode node = root;
        while (node != null) {
            if (val > node.val) {
                if (node.right == null) {
                    node.right = new TreeNode(val);
                    return root;
                } else
                    node = node.right;
            } else {
                if (node.left == null) {
                    node.left = new TreeNode(val);
                    return root;
                } else
                    node = node.left;
            }
        }
        return new TreeNode(val);
    }
}

Python:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        node = root
        while node:
            if val > node.val:
                if not node.right:
                    node.right = TreeNode(val)
                    return root
                else:
                    node = node.right
            else:
                if not node.left:
                    node.left = TreeNode(val)
                    return root
                else:
                    node = node.left
        return TreeNode(val)

Golang:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func insertIntoBST(root *TreeNode, val int) *TreeNode {
    t := new(TreeNode)
    t.Val = val
    node := root
    for node != nil {
        if val > node.Val {
            if node.Right == nil {
                node.Right = t
                return root
            } else {
                node = node.Right
            }
        } else {
            if node.Left == nil {
                node.Left = t
                return root
            } else {
                node = node.Left
            }
        }
    }
    return t
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-03-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 爱写Bug 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
48 Insert into a Binary Search Tree
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
devi
2021/08/18
2610
【一天一大 lee】二叉搜索树中的插入操作 (难度:中等) - Day20200930
给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。返回插入后二叉搜索树的根节点。输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。
前端小书童
2020/10/26
4030
【一天一大 lee】二叉搜索树中的插入操作 (难度:中等) - Day20200930
LwwtCode 173:二叉搜索树迭代器 Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
爱写bug
2020/03/12
4790
LeetCode 700: 二叉搜索树中的搜索 Search in a Binary Search Tree
给定二叉搜索树(BST)的根节点和一个值。你需要在BST中找到节点值等于给定值的节点。返回以该节点为根的子树。如果节点不存在,则返回 NULL。
爱写bug
2020/03/25
4790
数据结构与算法(3)——树(二叉、二叉搜索树)树LeetCode相关题目整理
树是一种类似于链表的数据结构,不过链表的结点是以线性方式简单地指向其后继结点,而树的一个结点可以指向许多个结点;数是一种典型的非线性结构;树结构是以表达具有层次特性的图结构的一种方法;
我没有三颗心脏
2018/07/24
1.1K0
数据结构与算法(3)——树(二叉、二叉搜索树)树LeetCode相关题目整理
二叉搜索树中的插入操作
题目链接:https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/
代码随想录
2021/09/08
4230
二叉搜索树中的插入操作
leetcode刷题(21)——98. 验证二叉搜索树
节点的左子树只包含小于当前节点的数。 节点的右子树只包含大于当前节点的数。 所有左子树和右子树自身必须也是二叉搜索树。 示例 1:
老马的编程之旅
2022/06/22
1780
4个例子,吃透 JavaScript 实现的二叉搜索树 BST
对于每一个节点root,代码值检查了它的左右孩子节点是否符合左小右大的原则;但是根据 BST 的定义,root的整个左子树都要小于root.val,整个右子树都要大于root.val。
程序员小助手
2022/12/20
3500
Python算法——二叉搜索树
二叉搜索树是一种常见的树状数据结构,具有有序性质。在二叉搜索树中,每个节点的值大于其左子树中的任何节点值,小于其右子树中的任何节点值。这种有序性质使得二叉搜索树具有高效的查找、插入和删除操作。在本文中,我们将深入探讨二叉搜索树的原理,并提供Python代码实现。
Echo_Wish
2023/11/30
2440
LeetCode 450: 删除二叉搜索树中的节点 Delete Node in a BST
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
爱写bug
2020/03/25
1.2K0
二叉排序树:数据存储的艺术
👋 你好,我是 Lorin 洛林,一位 Java 后端技术开发者!座右铭:Technology has the power to make the world a better place.
Lorin 洛林
2023/11/13
2460
二叉排序树:数据存储的艺术
LeetCode 29:验证二叉搜索树 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).
爱写bug
2020/03/12
3250
TypeScript算法题实战——二叉搜索树篇
推荐文章:《Spring AI中的卷积神经网络(CNN):深度解析与Java实现》
中杯可乐多加冰
2024/12/08
1130
【算法题解】 Day9 二叉搜索树
我们可以从题目中知道何为有效的二叉搜索树,父节点的值要大于左孩子且小于右孩子,同时所有左子树和右子树自身必须也是二叉搜索树。
sidiot
2023/08/31
1490
【算法题解】 Day9 二叉搜索树
LeetCode - 二叉搜索树中的插入操作
原题地址:https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/
晓痴
2019/08/09
4440
LeetCode - 二叉搜索树中的插入操作
​LeetCode刷题实战450:删除二叉搜索树中的节点
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/11/29
3380
​LeetCode刷题实战450:删除二叉搜索树中的节点
Swift 验证二叉搜索树- LeetCode
对isValidBSTUtil(node.left, min, node.val) && isValidBSTUtil(node.right, node.val, max) 取一个切片: isValidBSTUtil(node.left, min, node.val) 此时,
韦弦zhy
2018/12/19
9620
【leetcode刷题】T154-二叉搜索树中的插入操作
https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/
木又AI帮
2019/09/03
4310
二叉搜索树的最近公共祖先
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
木子星兮
2020/07/17
4360
98 验证二叉搜索树
和上题一样可以很好的想到递归的思路,左边都是越来越小,右边是越来越大。这个地方容易产生一种错觉。
木瓜煲鸡脚
2021/01/18
4780
98 验证二叉搜索树
推荐阅读
相关推荐
48 Insert into a Binary Search Tree
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文