import java.util.ArrayList; import java.util.*; /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) { ArrayList<ArrayList<Integer>> res = new ArrayList<>(); if(pRoot == null){ return res; } Queue<TreeNode> queue = new LinkedList<>(); queue.add(pRoot); boolean isLeft = true; while(!queue.isEmpty()){ int size = queue.size(); ArrayList<Integer> list = new ArrayList<>(); 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(isLeft){ res.add(list); }else{ Collections.reverse(list); res.add(list); } isLeft = !isLeft; } return res; } }