import java.util.ArrayList; import java.util.Collections; /* 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 > Print(TreeNode pRoot) { ArrayList<ArrayList> list = new ArrayList<>(); depth(pRoot,1,list); for(int i = 0;i < list.size();i++){ if(i % 2 != 0){ Collections.reverse(list.get(i)); } } return list;
}
public void depth(TreeNode root,int depth,ArrayList<ArrayList<Integer>> list){
if(root == null){
return;
}
if(depth > list.size()){
list.add(new ArrayList<Integer>());
}
list.get(depth - 1).add(root.val);
depth(root.left,depth + 1,list);
depth(root.right,depth + 1,list);
}
}