Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Roger7410/5fc463d264a71838249b98cde53be637 to your computer and use it in GitHub Desktop.

Select an option

Save Roger7410/5fc463d264a71838249b98cde53be637 to your computer and use it in GitHub Desktop.
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
boolean order = true;
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> tmpList = new ArrayList<>();
for(int i=0; i < size; i++){
TreeNode tmpNode = queue.poll();
if(order){
tmpList.add(tmpNode.val);
}else{
tmpList.add(0,tmpNode.val);
}
if(tmpNode.left!=null) queue.add(tmpNode.left);
if(tmpNode.right!=null) queue.add(tmpNode.right);
}
order = order ? false : true;
res.add(tmpList);
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment