如题,用个flag标志切换方向

public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        if (pRoot == null) {
            return result;
        }
        boolean flag = true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(pRoot);
        int count = 0;
        while (!queue.isEmpty()) {
            count = queue.size();
            ArrayList<Integer> list = new ArrayList<>();
            ArrayList<TreeNode> tmpList = new ArrayList<>();
            while (count > 0) {
                TreeNode node = queue.poll();
                list.add(node.val);
                tmpList.add(node);
                count--;
                if(count<=0){
                    if(flag){
                        flag = false;
                        if(tmpList.size()>0){
                            for(int i=tmpList.size()-1;i>=0;i--){
                                TreeNode node1 = tmpList.get(i);
                                if(node1.right!=null){
                                    queue.offer(node1.right);
                                }
                                if(node1.left!=null){
                                    queue.offer(node1.left);
                                }
                            }
                        }
                    }else{
                        if(tmpList.size()>0){
                            for(int i=tmpList.size()-1;i>=0;i--){
                                TreeNode node2 = tmpList.get(i);
                                if(node2.left!=null){
                                    queue.offer(node2.left);
                                }
                                if(node2.right!=null){
                                    queue.offer(node2.right);
                                }
                            }
                        }
                        flag = true;
                    }
                }
            }
            result.add(list);
        }
        return result;
    }