2019-11-02 12:07:41 +08:00
|
|
|
# 32.2 把二叉树打印成多行
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288?tpId=13&tqId=11213&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
|
|
|
|
|
|
|
|
## 题目描述
|
|
|
|
|
|
|
|
和上题几乎一样。
|
|
|
|
|
|
|
|
## 解题思路
|
|
|
|
|
|
|
|
```java
|
|
|
|
ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
|
|
|
|
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
|
|
|
|
Queue<TreeNode> queue = new LinkedList<>();
|
|
|
|
queue.add(pRoot);
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
ArrayList<Integer> list = new ArrayList<>();
|
|
|
|
int cnt = queue.size();
|
|
|
|
while (cnt-- > 0) {
|
|
|
|
TreeNode node = queue.poll();
|
|
|
|
if (node == null)
|
|
|
|
continue;
|
|
|
|
list.add(node.val);
|
|
|
|
queue.add(node.left);
|
|
|
|
queue.add(node.right);
|
|
|
|
}
|
|
|
|
if (list.size() != 0)
|
|
|
|
ret.add(list);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
```
|
2019-11-02 14:39:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-1.png"></img></div>
|