We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5161ef7 commit bbbd218Copy full SHA for bbbd218
swift/102-Binary-Tree-Level-Order-Traversal.swift
@@ -0,0 +1,28 @@
1
+class Solution {
2
+ func levelOrder(_ root: TreeNode?) -> [[Int]] {
3
+ guard let root = root else { return [] }
4
+ var queue = [TreeNode]()
5
+ var result = [[Int]]()
6
+ queue.append(root)
7
+ while !queue.isEmpty {
8
+ var levelCount = queue.count
9
+ var levelNodes = [Int]()
10
+ while levelCount > 0 {
11
+ let node = queue.first!
12
+ queue.removeFirst()
13
+ levelCount -= 1
14
+ levelNodes.append(node.val)
15
+ if let left = node.left {
16
+ queue.append(left)
17
+ }
18
+
19
+ if let right = node.right {
20
+ queue.append(right)
21
22
23
+ result.append(levelNodes)
24
25
+ return result
26
27
28
+}
0 commit comments