class Solution {
public:
//本质还是bfs,奇数行从左到右,偶数行从右到左
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> >result;
queue<TreeNode*>q;
int level=0;
if(pRoot==NULL)return result;//防止为空
q.push(pRoot);
while(!q.empty()){
vector<int>ans;
int size=q.size();//用于记录每一行个数
while(size--){
TreeNode* temp=q.front();
q.pop();
ans.push_back(temp->val);
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
}
level++;
if(!(level&1)){reverse(ans.begin(),ans.end());}//偶数要反转
result.push_back(ans);
}
return result;
}
};