public static ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        // write your code here
        Queue<TreeNode> q=new ArrayDeque<>();
        ArrayList<ArrayList<Integer>> rel=new ArrayList<>();
        if (root!=null)
            q.offer(root);
        while (!q.isEmpty()) {
            ArrayList<Integer>list=new ArrayList<>();
            for (int i=0, n=q.size();i<n;++i) {
                TreeNode c=q.poll();
                list.add(c.val);
                if (c.left!=null)
                    q.offer(c.left);
                if (c.right!=null)
                    q.offer(c.right);
            }
            rel.add(list);
        }
        return rel;
    }