# 按之字形顺序打印二叉树

• 时间限制：1秒空间限制：32768K
• 通过比例：30.04%
• 最佳记录：0 ms|8552

## 题目描述

/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > ret;
if(!pRoot) return ret;
vector<int> curLayer;
vector<TreeNode*> LevelSearch;
TreeNode *cur=pRoot;
LevelSearch.push_back(cur);
int start=0,end=0;
bool LR=false;
do{
for(int i=start;i<=end;++i){
if(LevelSearch[i]->left) LevelSearch.push_back(LevelSearch[i]->left);
if(LevelSearch[i]->right) LevelSearch.push_back(LevelSearch[i]->right);
curLayer.push_back(LevelSearch[i]->val);
}
if(LR){
int temp=-1;
for(int i=0,j=curLayer.size()-1;i<j;++i,--j){
temp=curLayer[i];
curLayer[i]=curLayer[j];
curLayer[j]=temp;
}
}
ret.push_back(curLayer);
LR=!LR;
curLayer.clear();
start=end+1;
end=LevelSearch.size()-1;
}while(start<=end);

return ret;
}
};

07-19 1056

11-05 2172

06-06 1445

03-30 96

04-18 540

07-13 3340

02-24 20

12-04 322

03-02 42

03-28 52