# 二叉树的锯齿形层序遍历
给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回锯齿形层序遍历如下:
[
[3],
[20,9],
[15,7]
]
## template
```java
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public List> zigzagLevelOrder(TreeNode root) {
List> list = new LinkedList<>();
if (root == null) {
return list;
}
Stack stack1 = new Stack<>();
stack1.push(root);
boolean postive = true;
while (!stack1.isEmpty()) {
Stack stack2 = new Stack<>();
List subList = new LinkedList<>();
while (!stack1.isEmpty()) {
TreeNode current = stack1.pop();
subList.add(current.val);
if (postive) {
if (current.left != null) {
stack2.push(current.left);
}
if (current.right != null) {
stack2.push(current.right);
}
} else {
if (current.right != null) {
stack2.push(current.right);
}
if (current.left != null) {
stack2.push(current.left);
}
}
}
postive = !postive;
stack1 = stack2;
list.add(subList);
}
return list;
}
}
```
## 答案
```java
```
## 选项
### A
```java
```
### B
```java
```
### C
```java
```