public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
self.right = right
}
}
class Solution {
func levelOrder(_ root: TreeNode?) -> [[Int]] {
var someInts = [[Int]]()
if let node = root{
var arr = [node]
while arr.count > 0 {
var tmpNods = [TreeNode]()
var tmp = [Int]()
for node in arr {
tmp.append(node.val)
if let left = node.left{
tmpNods.append(left)
}
if let right = node.right{
tmpNods.append(right);
}
}
arr = tmpNods
someInts.append(tmp)
}
}
return someInts
}
}
|