敢教日月换新天。为有牺牲多壮志,

[Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10502896.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

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

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

例如, 

给定二叉搜索树:

        4
       / \
      2   7
     / \
    1   3

和 插入的值: 5

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

         4
       /   \
      2     7
     / \   /
    1   3 5

或者这个树也是有效的:

         5
       /   \
      2     7
     / \   
    1   3
         \
          4

192ms
复制代码
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func insertIntoBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
16         var found = false
17         guard var node = root else{ return nil}        
18         while found == false{
19             if node.val > val{                
20                 if let l = node.left {
21                     node = node.left!
22                 }else{
23                      node.left = TreeNode(val)
24                     found = true
25                 }                
26             }else{
27                 if let r = node.right {
28                     node = node.right!
29                 }else{
30                     node.right = TreeNode(val)
31                     found = true
32                 }
33             }
34         }        
35         return root
36     }
37 }
复制代码

Runtime: 196 ms
Memory Usage: 20.2 MB
复制代码
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func insertIntoBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
16         if root == nil {return TreeNode(val)}
17         if root!.val > val
18         {
19             root?.left = insertIntoBST(root?.left, val)
20         }
21         else
22         {
23             root?.right = insertIntoBST(root?.right, val)
24         }
25         return root
26     }
27 }
复制代码

200ms

复制代码
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func insertIntoBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
16         // let newnode = TreeNode()
17         // newnode.val = val
18         guard let root = root else { return TreeNode(val) }
19        
20         //go to left subtree
21         if val < root.val {
22             if root.left == nil { 
23                 root.left = TreeNode(val)
24             }else{
25                 insertIntoBST(root.left, val)
26             }
27             
28         }
29         //go to right subtree
30         if val > root.val {
31             if root.right == nil {
32                 root.right = TreeNode(val)
33             }else{
34                 insertIntoBST(root.right, val)
35             }
36             
37         }
38         
39         return root
40     }
41 }
复制代码

216ms

复制代码
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func insertIntoBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
16         guard let root = root else { return TreeNode(val) }
17         
18         var curr: TreeNode? = root
19         var prev: TreeNode = root
20         while let node = curr { 
21             if node.val > val { 
22                 curr = node.left
23             } else { curr = node.right }
24             prev = node
25         }
26         
27         if prev.val > val {
28             prev.left = TreeNode(val)
29         } else { 
30             prev.right = TreeNode(val)
31         }
32         return root
33     }
34 }
复制代码

 

posted @   为敢技术  阅读(364)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°