import java.util.ArrayList; /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ import java.util.*; public class Solution { public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) { ArrayList<ArrayList<Integer>> arrayList = new ArrayList<ArrayList<Integer>>(); if( pRoot == null ){ return arrayList; } Stack<TreeNode> s1 = new Stack<TreeNode>(); s1.add( pRoot ); Stack<TreeNode> s2 = new Stack<TreeNode>(); while( !s1.empty() || !s2.empty()){ if(!s1.empty()){ ArrayList<Integer> list = new ArrayList<Integer>(); while(!s1.empty()){ TreeNode node = s1.pop(); list.add(node.val); if(node.left != null){ s2.add(node.left); } if(node.right != null){ s2.add(node.right); } } arrayList.add(list); }else{ ArrayList<Integer> list = new ArrayList<Integer>(); while(!s2.empty()){ TreeNode node = s2.pop(); list.add(node.val); if(node.right != null){ s1.add(node.right); } if(node.left != null){ s1.add(node.left); } } arrayList.add(list); } } return arrayList; } }