37 lines
1.2 KiB
Markdown
37 lines
1.2 KiB
Markdown
|
# 32.3 按之字形顺序打印二叉树
|
||
|
|
||
|
[NowCoder](https://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0?tpId=13&tqId=11212&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
|
||
|
|
||
|
## 题目描述
|
||
|
|
||
|
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
|
||
|
|
||
|
## 解题思路
|
||
|
|
||
|
```java
|
||
|
public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
|
||
|
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
|
||
|
Queue<TreeNode> queue = new LinkedList<>();
|
||
|
queue.add(pRoot);
|
||
|
boolean reverse = false;
|
||
|
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 (reverse)
|
||
|
Collections.reverse(list);
|
||
|
reverse = !reverse;
|
||
|
if (list.size() != 0)
|
||
|
ret.add(list);
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
```
|