import java.util.ArrayList;

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
//一个队列 + 一个flag 就够了
import java.util.*;
public class Solution {
    public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        Queue<TreeNode> q = new LinkedList<>();
        q.add(pRoot);
        ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
        Boolean leftToRight = false;
                if(pRoot == null) return ans;
        while(!q.isEmpty()) {
            ArrayList<Integer> cur = new ArrayList<>();
            int size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = q.poll();
                if(node.left != null) q.add(node.left);
                if(node.right != null) q.add(node.right);
                if(!leftToRight){
                    cur.add(node.val);
                } else {
                    cur.add(0, node.val);
                }
            }
            leftToRight = !leftToRight;
            ans.add(cur);
        }
        return ans;
    }
}