class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > ans;
        if(!pRoot) return ans;
        queue<TreeNode *> q;
        q.push(pRoot);
        int flag = 1;  // 标记是否翻转
        while(!q.empty()){
            int size = q.size();
            flag = flag^1; 
            vector<int> tmp;
            while(size--){
                TreeNode *r = q.front();
                q.pop();
                tmp.push_back(r->val);
                if(r->left) q.push(r->left);
                if(r->right) q.push(r->right);
            }
            if(flag) reverse(tmp.begin(), tmp.end()); // 翻转
            ans.push_back(tmp);
        }

        return ans;

    }

};