> For the complete documentation index, see [llms.txt](https://ryukiedev.gitbook.io/wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ryukiedev.gitbook.io/wiki/shu-ju-jie-gou-yu-suan-fa/jian-zhi-offerswift/32ii.-cong-shang-dao-xia-da-yin-er-cha-shu-ii.md).

# 32.II.从上到下打印二叉树II

## 题目

从上到下按层打印二叉树，同一层的节点按从左到右的顺序打印，每一层打印到一行。

例如:

给定二叉树: \[3,9,20,null,null,15,7],

```
     3
    / \
   9  20
     /  \
    15   7
```

返回其层次遍历结果：

```
 [
   [3],
   [9,20],
   [15,7]
 ]
```

提示：

节点总数 <= 1000

注意：本题与主站 102 题相同：<https://leetcode-cn.com/problems/binary-tree-level-order-traversal/>

来源：力扣（LeetCode）

链接：<https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof>

## 题解

```swift
func levelOrder(_ root: TreeNode?) -> [[Int]] {
    var queue: [TreeNode] = []
    var toBePrint = 0
    var nextLevelCount = 0
    var result: [[Int]] = []
    var temp: [Int] = []

    if let r = root {
        queue.append(r)
        toBePrint += 1
    }

    while queue.isEmpty == false {
        if let first = queue.first {
            temp.append(first.val)
            queue.removeFirst()
            toBePrint -= 1

            if let left = first.left {
                queue.append(left)
                nextLevelCount += 1
            }

            if let right = first.right {
                queue.append(right)
                nextLevelCount += 1
            }

            if toBePrint == 0 {
                result.append(temp)
                temp = []

                toBePrint = nextLevelCount
                nextLevelCount = 0
            }
        }
    }

    if temp.isEmpty == false {
        result.append(temp)
    }

    return result
}
```

![2](https://4193904735-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MI8JgbGh3U6X_oedqkm%2Fsync%2Fde0c0101e95fdf006a0a081d63aacf25184469ff.png?generation=1630498066054168\&alt=media)
