/**
Definition for a binary tree node.*/
import java.util.*;
int val;
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
public List
<List
<Integer
>> zigzagLevelOrder
(TreeNode root
) { List<List<Integer>> output = new ArrayList<>();
if(root == null)
return output;
Stack<TreeNode> parent = new Stack<>();
Stack<TreeNode> child = new Stack<>();
boolean leftToRight = true;
parent.add(root);
List<Integer> level = new ArrayList<>();
while(!parent.isEmpty()){
level.add(node.val);
if(leftToRight){
if(node.left!=null) child.add(node.left);
if(node.right!=null) child.add(node.right);
}
else{
if(node.right!=null) child.add(node.right);
if(node.left!=null) child.add(node.left);
}
if(parent.isEmpty()){
leftToRight = !leftToRight;
parent = child;
child = new Stack<>();
output.add(level);
level = new ArrayList<>();
}
}
return output;
}
public static void main
(String args
[]){
System.
out.
print(new Solution
().
zigzagLevelOrder(root
));
}
}