/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ #include <iterator> #include <vector> class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { vector<vector<int>> ans; vector<int>t; if(!pRoot) return ans; queue<TreeNode*> Q; Q.push(pRoot); while(!empty(Q)){ int size=Q.size(); while(size--){ pRoot=Q.front(); Q.pop(); t.push_back(pRoot->val); if(pRoot->left) Q.push(pRoot->left); if(pRoot->right) Q.push(pRoot->right); } ans.push_back(t); t.clear(); } for(int i=0;i<ans.size();i++){ if(i%2==1) reverse(ans[i].begin(),ans[i].end()); } return ans; } };