-
Notifications
You must be signed in to change notification settings - Fork 886
/
BinaryTreeZigzagLevelOrderTraversal.swift
42 lines (36 loc) · 1.18 KB
/
BinaryTreeZigzagLevelOrderTraversal.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Question Link: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
* Primary idea: use a queue to help hold TreeNode, and for each level add a new Int array
*
* Note: use a boolean value to determine if needs to be added reversely
*
* Time Complexity: O(n), Space Complexity: O(n)
*
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*
*/
class BinaryTreeZigzagLevelOrderTraversal {
func zigzagLevelOrder(root: TreeNode?) -> [[Int]] {
guard let root = root else {
return [[Int]]()
}
var res = [[Int]](), isReverse = false, nodeQ = [root]
while !nodeQ.isEmpty {
let currentLevel = nodeQ.map { $0.val }
res.append(isReverse ? currentLevel.reversed() : currentLevel)
isReverse = !isReverse
nodeQ = nodeQ.flatMap { [$0.left, $0.right].compactMap { $0 } }
}
return res
}
}