Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Yi Gu [Tree] refactor code style e5b1f8b Apr 10, 2016
1 contributor

Users who have contributed to this file

52 lines (45 sloc) 1.37 KB
/**
* Question Link: https://leetcode.com/problems/binary-tree-level-order-traversal/
* Primary idea: use a queue to help hold TreeNode, and for each level add a new Int array
* 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 BinaryTreeLevelOrderTraversal {
func levelOrder(root: TreeNode?) -> [[Int]] {
var res = [[Int]]()
var queue = [TreeNode]()
if let root = root {
queue.append(root)
}
while queue.count > 0 {
var size = queue.count
var level = [Int]()
for _ in 1...size {
let node = queue[0]
queue.removeAtIndex(0)
// add val
level.append(node.val)
// add TreeNodes in next level
if let left = node.left {
queue.append(left)
}
if let right = node.right {
queue.append(right)
}
}
res.append(level)
}
return res
}
}
You can’t perform that action at this time.