/*
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>> result = new ArrayList<>();
        if(pRoot == null){
            return result;
        }

        Stack<TreeNode> temp1 = new Stack<>();
        Stack<TreeNode> temp2 = new Stack<>();
        temp1.push(pRoot);
        
        while(!temp1.isEmpty() || !temp2.isEmpty()){
            ArrayList<Integer> line1 = new ArrayList<>();
            ArrayList<Integer> line2 = new ArrayList<>();
            while(!temp1.isEmpty()){
                TreeNode node = temp1.pop();
                line1.add(node.val);
                if(node.left != null){
                    temp2.push(node.left);
                }
                if(node.right != null){
                    temp2.push(node.right);
                }
                
            }
            while(!temp2.isEmpty()){
                TreeNode node = temp2.pop();
                line2.add(node.val);
                if(node.right != null){
                    temp1.push(node.right);
                }
                if(node.left != null){
                    temp1.push(node.left);
                }
            }
            
            if(!line1.isEmpty()){
                result.add(line1);
            }
            if(!line2.isEmpty()){
                result.add(line2);
            }
            
        }
        return result;
    }

}