This is my solution for 102nd Binary Tree Level Order Traversal in Swift

102. Binary order level traversalReturns a binary tree and returns the layer order of the node values. (ie from left to right, level by level).[3]Given binary tree [3,9,20,null,null,15,7],

3

/

9 20

/

15 7

Returns the level order run as:

[[[[

,

[9,20],

[15,7]

]

Here is my code:

```
func levelOrder (_root: TreeNode?) -> [[Int]]{
Guard leave root = root other {
return []
}
let node = recursiveVisit (root)
let Capacity = Node.reduce ([Int]()) {
if $ 0. contains ($ 1,0) == wrong {
Return $ 0 + [$1.0]
}
Return $ 0
}.Number
return node.reduce ([[Int]](Repeat: []Number: capacity), {
var tmp = $ 0
tmp[$1.0].append ($ 1,1)
Return tmp
})
}
func recursiveVisit (_ node: TreeNode?) -> [(Int, Int)]{
// [(Int, Int)]
// depth, node.value
guard let node = node else {
return []
}
var node = [(Int, Int)]()
node.append ((0, node.val))
let lhs = recursiveVisit (node.left) .map {
Yield ($ 0.0 + 1, $ 0.1)
}
let rhs = recursiveVisit (node.right) .map {
Yield ($ 0.0 + 1, $ 0.1)
}
node.append (contentsOf: lhs)
node.append (contentsOf: rhs)
return node
}
```

The solution is very intuitive, collect the value and depth of the nodes. Then reduce the answer.

Not well designed as a Java solution

My question is mainly about the Swift syntax.

Get the information, reduce it first to the number and then to the collection.

Is there another way to make it leaner? Combine the two to reduce to one.