1.队列法
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
#include<queue>
class Solution {
public:
vector<vector<int>> Print(TreeNode* pRoot) {
vector<vector<int>> res = vector<vector<int>>();
queue<TreeNode*> q = queue<TreeNode*>();
int mark = 1;
if(pRoot) {
q.push(pRoot);
}
while(!q.empty()) {
vector<int> current = vector<int>();
int n = q.size();
for(int i =0;i<n;i++) {
TreeNode* node = q.front();
q.pop();
current.push_back(node->val);
if(node->left){
q.push(node->left);
}
if(node->right){
q.push(node->right);
}
}
if(mark%2==0){
reverse(current.begin(), current.end());
}
mark++;
res.push_back(current);
}
return res;
}
};