[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 }
相關文章
相關標籤/搜索