题解 | #按之字形顺序打印二叉树#
按之字形顺序打印二叉树
https://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0
class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { vector<vector<int>> ans; if(!pRoot) return ans; queue<TreeNode*> q; q.push(pRoot); int n,i,step=0; TreeNode* t; while(!q.empty()) { n = q.size(); vector<int> tmp; for(i=0;i<n;i++) { t = q.front(); tmp.push_back(t->val); q.pop(); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } step++; if(step%2==0) reverse(tmp.begin(), tmp.end()); ans.push_back(tmp); } return ans; } };