[Swift]LeetCode450. 刪除二叉搜索樹中的節點 | Delete Node in a BST

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-tjqupcxw-gw.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.node

Basically, the deletion can be divided into two stages:git

  1. Search for a node to remove.
  2. If the node is found, delete the node. 

Note: Time complexity should be O(height of tree).github

Example:算法

root = [5,3,6,2,4,null,7]
key = 3

    5
   / \
  3   6
 / \   \
2   4   7

Given key to delete is 3. So we find the node with value 3 and delete it.

One valid answer is [5,4,6,2,null,null,7], shown in the following BST.

    5
   / \
  4   6
 /     \
2       7

Another valid answer is [5,2,6,null,4,null,7].

    5
   / \
  2   6
   \   \
    4   7

給定一個二叉搜索樹的根節點 root 和一個值 key,刪除二叉搜索樹中的 key 對應的節點,並保證二叉搜索樹的性質不變。返回二叉搜索樹(有可能被更新)的根節點的引用。微信

通常來講,刪除節點可分爲兩個步驟:ide

  1. 首先找到須要刪除的節點;
  2. 若是找到了,刪除它。

說明: 要求算法時間複雜度爲 O(h),h 爲樹的高度。spa

示例:code

root = [5,3,6,2,4,null,7]
key = 3

    5
   / \
  3   6
 / \   \
2   4   7

給定須要刪除的節點值是 3,因此咱們首先找到 3 這個節點,而後刪除它。

一個正確的答案是 [5,4,6,2,null,null,7], 以下圖所示。

    5
   / \
  4   6
 /     \
2       7

另外一個正確答案是 [5,2,6,null,4,null,7]。

    5
   / \
  2   6
   \   \
    4   7

152ms
 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 deleteNode(_ root: TreeNode?, _ key: Int) -> TreeNode? {
16         /*
17          *  思路:
18          *  1,找到須要被刪除的節點
19          *  2,沒有子節點,直接刪除
20          *  3,一個子節點,直接替換
21          *  4,兩個子節點,中序遍歷的第一個節點替換,BST的中序遍歷就是排好序的
22          */
23         if root == nil {return root}
24         var temp = root
25         // 在左子樹中尋找刪除
26         if temp!.val > key{
27         
28             temp?.left = deleteNode(temp?.left, key)
29         }else if temp!.val < key{
30             // 在右子樹中刪除
31             temp?.right = deleteNode(temp?.right, key)
32         }else{
33             // 找到了須要刪除的節點,若是節點有一個子節點
34             if temp?.left == nil || temp?.right == nil{
35                 temp = temp?.left == nil ? temp?.right : temp?.left
36             }else{
37                 var node = temp!.right!
38                 while node.left != nil{
39                     node = node.left!
40                 }
41                 temp!.val = node.val
42                 temp?.right = deleteNode(temp?.right, node.val)
43             }
44         }
45         return temp
46     }
47 }

 164mshtm

 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 deleteNode(_ root: TreeNode?, _ key: Int) -> TreeNode? {
16         guard let root = root else {
17             return nil
18         }
19         
20         if key < root.val {
21             root.left = deleteNode(root.left, key)
22         } else if key > root.val {
23             root.right = deleteNode(root.right, key)
24         } else {
25             if root.left == nil {
26                 return root.right
27             } else if root.right == nil {
28                 return root.left
29             } else {
30                 let minNode = findMin(root.right!)
31                 root.val = minNode.val
32                 root.right = deleteNode(root.right, root.val)
33             }
34         }
35         
36         return root
37     }
38     
39     func findMin(_ root: TreeNode) -> TreeNode {
40         var root = root
41         
42         while let leftNode = root.left {
43             root = leftNode
44         }
45         
46         return root
47     }
48 }

184ms

 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 deleteNode(_ root: TreeNode?, _ key: Int) -> TreeNode? {
16         var root = root
17         if root == nil {return nil}
18         if root!.val == key
19         {
20             if root!.right == nil {return root!.left}
21             else
22             {
23                 var cur:TreeNode? = root!.right
24                 while(cur?.left != nil)
25                 {
26                     cur = cur!.left          
27                 }
28                 (root!.val,cur!.val) = (cur!.val,root!.val) 
29             }
30         }
31         root!.left = deleteNode(root!.left, key)
32         root!.right = deleteNode(root!.right, key)
33         return root
34     }
35 }
相關文章
相關標籤/搜索