题解 | #按之字形顺序打印二叉树#

https://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0

```import java.util.*;

/*
* public class TreeNode {
*   int val = 0;
*   TreeNode left = null;
*   TreeNode right = null;
*   public TreeNode(int val) {
*     this.val = val;
*   }
* }
*/

public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return int整型ArrayList<ArrayList<>>
*/
public ArrayList<ArrayList<Integer>> Print (TreeNode pRoot) {
// write code here
if(pRoot==null){
return new ArrayList<>();
}
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
boolean isleft = true;
ArrayDeque<TreeNode> q = new ArrayDeque<>();
while (!q.isEmpty()) {
ArrayList<Integer>row = new ArrayList<>();
int n = q.size();
if (isleft) {
for (int i = 0; i < n; i++) {
TreeNode cur = q.pollFirst();
//若是左右孩子存在，则存入左右孩子作为下一个层次
if (cur.left != null)
if (cur.right != null)
}
isleft = false;
} else {
for (int i = n-1; i >= 0; i--) {
TreeNode cur = q.pollLast();
if (cur.right != null)
if (cur.left != null)

}
isleft = true;
}

}
return res;
}
}
```

12-07 14:14
Java