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

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

作者头像
爱写bug
发布2020-03-25 16:54:23
9340
发布2020-03-25 16:54:23
举报
文章被收录于专栏:爱写Bug爱写Bug

题目:

给定二叉搜索树(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
复制
给定二叉搜索树:

        4
       / \
      2   7
     / \
    1   3

和 插入的值: 5

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

代码语言:javascript
复制
         4
       /   \
      2     7
     / \   /
    1   3 5

或者这个树也是有效的:

代码语言:javascript
复制
         5
       /   \
      2     7
     / \   
    1   3
         \
          4

解题思路:

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

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

递归法:

Java:

代码语言:javascript
复制
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
复制
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
复制
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
复制
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
复制
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
复制
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 删除。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目:
  • 解题思路:
  • 递归法:
  • 迭代法:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档