Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{publicList<List<Integer>>zigzagLevelOrder(TreeNoderoot){List<List<Integer>>ret=LinkedList<List<Integer>>();travel(root,ret,0);}publicvoidtravel(TreeNodenode,List<List<Integer>>ret,intlevel){if(node==null)return;if(ret.size()<=level){List<Integer>newLevel=newLinkedList<Integer>();ret.add(newLevel);}List<Integer>collection=ret.get(level);if(level%2==0)collection.add(node.val);elsecollection.add(0,node.val);travel(node.left,ret,level+1);travel(node.right,ret,level+1);}}