LeetCode | 0701. 二叉搜索树中的插入操作【Python】

LeetCode 0701. Insert into a Binary Search Tree 二叉搜索树中的插入操作【Medium】【Python】【二叉树】

Problem

LeetCode

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.

For example,

Given the tree:
        4
       / \
      2   7
     / \
    1   3
And the value to insert: 5

You can return this binary search tree:

         4
       /   \
      2     7
     / \   /
    1   3 5

This tree is also valid:

         5
       /   \
      2     7
     / \   
    1   3
         \
          4

Constraints:

  • The number of nodes in the given tree will be between 0 and 10^4.
  • Each node will have a unique integer value from 0 to -10^8, inclusive.
  • -10^8 <= val <= 10^8
  • It's guaranteed that val does not exist in the original BST.

问题

力扣

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

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

例如,

给定二叉搜索树:

    4
   / \
  2   7
 / \
1   3

和 插入的值: 5
你可以返回这个二叉搜索树:

     4
   /   \
  2     7
 / \   /
1   3 5

或者这个树也是有效的:

     5
   /   \
  2     7
 / \   
1   3
     \
      4

思路

二叉树

Python3代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        # 找到空位置
        if not root:
            return TreeNode(val)
        if root.val < val:
            root.right = self.insertIntoBST(root.right, val)
        if root.val > val:
            root.left = self.insertIntoBST(root.left, val)
        return root

GitHub链接

Python

LeetCode个人题解 文章被收录于专栏

LeetCode个人题解,目前主要是 Python3 题解。

全部评论

相关推荐

哞客37422655...:兄弟别慌!💪 民办本找实习确实难点,但不是没机会。100+简历才2个面试,可能简历需要优化下: 项目经历写具体点,突出测试用例、bug数量等 技能栏把测试工具/方法论写清楚 可以考虑降低预期,先进小厂积累经验 测试岗相对好进,坚持投!现在才半个月,有人投3个月才上岸的😭 加油,offer在路上了🚀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务