1. 程式人生 > >[Swift]LeetCode94. 二叉樹的中序遍歷 | Binary Tree Inorder Traversal

[Swift]LeetCode94. 二叉樹的中序遍歷 | Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]

Follow up: Recursive solution is trivial, could you do it iteratively?


 給定一個二叉樹,返回它的中序 遍歷。

示例:

輸入: [1,null,2,3]
   1
    \
     2
    /
   3

輸出: [1,3,2]

進階: 遞迴演算法很簡單,你可以通過迭代演算法完成嗎?


 8ms

 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 inorderTraversal(_ root: TreeNode?) -> [Int] { 16 guard let root = root else { return [Int]() } 17 18 var ret = [Int]() 19 var worklist = [TreeNode]()
20 var n : TreeNode? = root 21 22 while !worklist.isEmpty || n != nil { 23 if n != nil { 24 worklist.append(n!) 25 n = n!.left 26 } else { 27 n = worklist.popLast() 28 ret.append(n!.val) 29 n = n!.right 30 } 31 } 32 33 return ret 34 } 35 }

8ms

 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 inorderTraversal(_ root: TreeNode?) -> [Int] {
16          return inorderTraversal_morris(root)
17     }
18     
19     func inorderTraversal_morris(_ r: TreeNode?) -> [Int] {
20         var root = r
21         if root == nil {
22             return []
23         } else {
24             var res: [Int] = []
25             var pre: TreeNode? = nil
26             while root != nil {
27                 if root?.left == nil {
28                     res.append((root?.val)!)
29                     root = root?.right
30                 } else {
31                     pre = root?.left
32                     while pre?.right != nil && pre?.right! !== root {
33                         pre = pre?.right
34                     }
35                     if pre?.right == nil {
36                         pre?.right = root
37                         root = root?.left
38                     } else {
39                         pre?.right = nil
40                         res.append((root?.val)!)
41                         root = root?.right
42                     }
43                 }
44             }
45             return res
46         }
47     }
48     // Recursion, t = O(N), average s = O(log(N)), worst s = O(N)
49     func inorderTraversal_iteration(_ root: TreeNode?) -> [Int] {
50         var res: [Int] = []
51         var stack: [TreeNode?] = []
52         var curr: TreeNode? = root
53         while curr != nil || stack.isEmpty == false {
54             while let unwrapped = curr {
55                 stack.append(unwrapped)
56                 curr = curr?.left
57             }
58             let last: TreeNode? = stack.removeLast()
59             res.append(last!.val)
60             curr = last!.right
61         }
62         return res
63     }
64     func inorderTraversal_recursion_helper(root: TreeNode?, arr: inout [Int]) {
65         guard let root = root else { return }
66         inorderTraversal_recursion_helper(root: root.left, arr: &arr)
67         arr.append(root.val)
68         inorderTraversal_recursion_helper(root: root.right, arr: &arr)
69     }
70     // Recursion, t = O(N), average s = O(log(N)), worst s = O(N)
71     func inorderTraversal_recursion(_ root: TreeNode?) -> [Int] {
72         var res: [Int] = []
73         inorderTraversal_recursion_helper(root: root, arr: &res)
74         return res
75     }
76 }

8ms

 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 inorderTraversal(_ root: TreeNode?) -> [Int] {
16         var arr = [Int]()
17         if root != nil && root!.left != nil {
18             arr.append(contentsOf:inorderTraversal(root!.left))
19         }
20         if root != nil { 
21             arr.append(root!.val) 
22         }
23         if root != nil && root!.right != nil {
24             arr.append(contentsOf:inorderTraversal(root!.right))
25         }
26         return arr
27     }
28 }

12ms

 1 class Solution {
 2     func inorderTraversal(_ root: TreeNode?) -> [Int] {
 3         var answer: [Int] = []
 4         inorder(root, &answer)
 5         return answer
 6     }
 7     
 8     func inorder(_ node: TreeNode?, _ answer: inout [Int]) {
 9         if let node = node {
10             inorder(node.left, &answer)
11             answer.append(node.val)
12             inorder(node.right, &answer)
13         }
14     }
15 }

16ms

 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 inorderTraversal(_ root: TreeNode?) -> [Int] {
16                 var result = [Int]()
17         var stack = [TreeNode]()
18         var pops = [TreeNode]()
19         var p = root
20         if p != nil {
21             stack.append(p!)
22         }else {
23             return result
24         }
25         
26         while (p != nil) {
27             
28             var isp = false
29             if let lastPop = pops.last {
30                 if lastPop === p! {
31                     pops.removeLast()
32                     isp = true
33                 }
34             }
35             let l =  p!.left
36             if  (l != nil) && !isp{
37                 stack.append(l!)
38                 pops.append(p!)
39             }else {
40                 result.append(p!.val)
41                 stack.removeLast()
42                 if let r = p!.right {
43                     stack.append(r)
44                 }
45             }
46             p = stack.last
47         }
48         
49         return result
50     }
51 }

24ms

 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 inorderTraversal(_ root: TreeNode?) -> [Int] {
16         var result : [Int] = []
17         if(root?.left != nil){
18             let leftResult : [Int]  =  inorderTraversal(root!.left)
19             result.append(contentsOf: leftResult)
20         }
21         
22         if(root != nil){
23             result.append(root!.val)
24         }
25      
26         if(root?.right != nil){
27             let rightResult : [Int]  =  inorderTraversal(root!.right)
28             result.append(contentsOf: rightResult)
29         }
30         return  result
31     }
32 }

68ms

 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 inorderTraversal(_ root: TreeNode?) -> [Int] {
16         var tRoot: TreeNode? = root
17         var stack: [TreeNode] = []
18         var result: [Int] = []
19         while tRoot != nil || stack.count > 0 {
20             if tRoot != nil {
21                 stack.append(tRoot!)
22                 tRoot = tRoot?.left
23             } else if stack.count > 0 {
24                 tRoot = stack.popLast()
25                 result.append(tRoot!.val)
26                 tRoot = tRoot?.right
27             }
28         }
29         return result
30     }
31 }