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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-kjuvufvs-me.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.node

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.git

For example, github

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:this

         5
       /   \
      2     7
     / \   
    1   3
         \
          4

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

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

例如, htm

给定二叉搜索树:

        4
       / \
      2   7
     / \
    1   3

和 插入的值: 5

你能够返回这个二叉搜索树:blog

         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 }
相关文章
相关标签/搜索