class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) return res; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); int level = 0; while (!queue.isEmpty()) { int size = queue.size(); level ++; List<Integer> list = new ArrayList<>(); for (int i = 0; i < size; i++) { TreeNode cur = queue.poll(); if ((level & 1) == 1) { list.add(cur.val); }else { list.add(0,cur.val); } if (cur.left != null) queue.offer(cur.left); if (cur.right != null) queue.offer(cur.right); } res.add(list); } return res; } }