1. 程式人生 > >[Swift]LeetCode897. 遞增順序查找樹 | Increasing Order Search Tree

[Swift]LeetCode897. 遞增順序查找樹 | Increasing Order Search Tree

重新 solution tco des note example prev not ext

Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.

Example 1:
Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]

       5
      /     3    6
   / \      2   4    8
 /        / \ 
1        7   9

Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]

 1
     2
         3
             4
                 5
                     6
                         7
                             8
                                 9  

Note:

  1. The number of nodes in the given tree will be between 1 and 100.
  2. Each node will have a unique integer value from 0 to 1000.

給定一個樹,按中序遍歷重新排列樹,使樹中最左邊的結點現在是樹的根,並且每個結點沒有左子結點,只有一個右子結點。

示例 :

輸入:[5,3,6,2,4,null,8,1,null,null,null,7,9]

       5
      /     3    6
   / \      2   4    8
 /        / \ 
1        7   9

輸出:[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]

 1
     2
         3
             4
                 5
                     6
                         7
                             8
                                 9   

提示:

  1. 給定樹中的結點數介於 1 和 100 之間。
  2. 每個結點都有一個從 0 到 1000 範圍內的唯一整數值。

Runtime: 88 ms Memory Usage: 19.4 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 increasingBST(_ root: TreeNode?) -> TreeNode? { 16 return increasingBST(root, nil) 17 } 18 19 func increasingBST(_ root:TreeNode?,_ tail:TreeNode?) -> TreeNode? { 20 if root == nil {return tail} 21 var res:TreeNode? = increasingBST(root?.left, root) 22 root?.left = nil 23 root?.right = increasingBST(root?.right, tail) 24 return res 25 } 26 }

88ms

 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 increasingBST(_ root: TreeNode?) -> TreeNode? {
16         return increasingBST(root, nil)        
17     }
18     
19     func increasingBST(_ root: TreeNode?, _ tail: TreeNode?) -> TreeNode? {
20         guard let node = root else {
21             return tail
22         }        
23         let left = increasingBST(node.left, node)
24         node.left = nil
25         node.right = increasingBST(node.right, tail)        
26         return left
27     }
28 }

96ms

 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     
16     var newParent: TreeNode?
17     var newRoot: TreeNode?
18     
19     func increasingBST(_ root: TreeNode?) -> TreeNode? {        
20         inorder(root)        
21         return newRoot
22     }
23     
24     func inorder(_ root: TreeNode?) {
25         guard let root = root else { return } 
26         inorder(root.left)        
27         if let newParent = newParent {
28             root.left = nil
29             newParent.right = root
30         } else {
31             newRoot = root
32         }        
33         newParent = root      
34         inorder(root.right)
35     }
36 }

100ms

 1 class Solution {
 2     var list = [TreeNode?]()
 3     func increasingBST(_ root: TreeNode?) -> TreeNode? {
 4         inorder(root: root)
 5         let node = TreeNode.init(0)
 6         list.insert(node, at: 0)
 7         for i in 0..<list.count - 1 {
 8             list[i]?.left = nil
 9             list[i]?.right = list[i+1]
10         }
11         if list.count - 1 > 0 {
12             list[list.count - 1]?.left = nil
13         }
14         return node.right
15     }
16     
17     func inorder(root: TreeNode?) {
18         var stack = [TreeNode]()
19         var root = root
20         while root != nil || stack.count > 0 {
21             if root != nil {
22                 stack.append(root!)
23                 root = root?.left
24             }else {
25                 let last = stack.removeLast()
26                 list.append(last)
27                 root = last.right
28             }
29         }
30     }
31 }

104ms

 1 class Solution {
 2     var newHead:TreeNode?
 3     var nextNode:TreeNode?
 4     
 5     
 6     func increasingBST(_ root: TreeNode?) -> TreeNode? {
 7 
 8         guard let root = root else
 9         {
10             return nil
11         }
12         
13         increasingBST(root.left)
14         
15         if newHead == nil
16         {
17             newHead = root
18         }
19         root.left = nil
20         if nextNode != nil
21         {
22             nextNode?.right = root
23         }
24         nextNode = root
25         
26         increasingBST(root.right)
27         return newHead
28     }
29 }

108ms

 1 class Solution {
 2     func increasingBST(_ root: TreeNode?) -> TreeNode? {
 3         guard let root = root else {
 4             return nil 
 5         }
 6         let resultTree = TreeNode(0)
 7         var current: TreeNode? = resultTree
 8         inorderTraversal(root) {
 9             val in 
10             current?.right = TreeNode(val)
11             current = current?.right
12         }
13         
14         return resultTree.right
15     }
16 }
17 
18 
19 func inorderTraversal(_ root: TreeNode?, _ visit: (Int) -> Void) {
20     guard let root = root else {
21         return 
22     }
23     inorderTraversal(root.left, visit)
24     visit(root.val)
25     inorderTraversal(root.right, visit)
26 }

116ms

 1 class Solution {
 2     func increasingBST(_ root: TreeNode?) -> TreeNode? {
 3         var toReturn = getLeft(root)
 4         var r = root
 5         helper(r, nil)
 6         return toReturn
 7     }
 8     
 9     func getLeft(_ root: TreeNode?) -> TreeNode? {
10         var root = root
11         if root == nil { return nil }
12         while root!.left != nil {
13             root = root!.left
14         }
15         return root
16     }
17     
18     func helper(_ root: TreeNode?, _ p: TreeNode?) -> TreeNode? {
19         if root == nil { return p }
20         let prev = helper(root!.left, p)
21         let r = root!.right
22         root!.left = nil
23         if var prev = prev {
24             prev.right = root
25         }
26         return helper(r, root)
27     }
28 }

216ms

 1 class Solution {
 2     func increasingBST(_ root: TreeNode?) -> TreeNode? {
 3        var arr = [Int]()
 4        midTree(root,&arr)
 5        if arr.count == 0 { return nil }
 6        else {
 7            var root = TreeNode(arr[0])
 8            var temp = root
 9            for index in 1..<arr.count {
10                var right = TreeNode(arr[index])
11                temp.right = right
12                temp = right
13            }
14            return root
15        }
16     }
17 
18     func midTree(_ root: TreeNode?, _ arr: inout [Int]) {
19         guard let root = root else { return }
20         if root.left != nil { midTree(root.left,&arr) }
21         arr.append(root.val)
22         if root.right != nil {midTree(root.right,&arr)}
23     }
24 }

[Swift]LeetCode897. 遞增順序查找樹 | Increasing Order Search Tree