import java.util.*;

/*
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<>();

        Queue<TreeNode> queue = new LinkedList<>();

        if (pRoot == null) {
            return result;
        }

        ArrayList<Integer> list = null;

        //true奇数层 false偶数层
        boolean flg = true;

        int level = 1;

        queue.offer(pRoot);

        while (!queue.isEmpty()) {

            int size = queue.size();

            list = new ArrayList<Integer>();

            for (int i = 0; i < size; i++) {

                TreeNode root = queue.poll();

                if (root.left != null) {
                    queue.offer(root.left);
                }

                if (root.right != null) {
                    queue.offer(root.right);
                }

                //偶数层 倒序往里放
                if (!flg) {
                    list.add(0, root.val);
                } else {
                    list.add(root.val);
                }
            }

            level++;

            if (level % 2 == 0) {
                flg = false;
            } else {
                flg = true;
            }

            result.add(list);
        }

        return result;
    }

}