[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 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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 }

 

转载于:https://www.cnblogs.com/strengthen/p/10502896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值