1 public class Solution { 2 public ArrayList> levelOrder(TreeNode root) { 3 ArrayList > res = new ArrayList >(); 4 LinkedList cur = new LinkedList (); 5 if(root==null) return res; 6 cur.offer(root); 7 while(!cur.isEmpty()){ 8 LinkedList next = new LinkedList (); 9 ArrayList sol = new ArrayList ();10 for(TreeNode n :cur){11 sol.add(n.val);12 if(n.left!=null) next.offer(n.left);13 if(n.right!=null) next.offer(n.right);14 }15 res.add(sol);16 cur = next;17 }18 return res;19 }20 }