为有牺牲多壮志,敢教日月换新天。

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9936920.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 

posted @ 2018-11-09 19:59  为敢技术  阅读(487)  评论(0编辑  收藏  举报