import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * * @param root TreeNode类 * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> zigzagLevelOrder (TreeNode root) { // write code here ArrayList<ArrayList<Integer>> res = new ArrayList<>(); if(root == null) return res; Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); boolean level = false; while(!queue.isEmpty()){ ArrayList<Integer> list = new ArrayList<>(); int size = queue.size(); for(int i = 0; i < size; ++i){ TreeNode node = queue.poll(); list.add(node.val); if(node.left != null){queue.add(node.left);} if(node.right != null){queue.add(node.right);} } if(level){ Collections.reverse(list); } level = !level; res.add(list); } return res; } }