void zzloHelp(vector<vector<int>> &res,unsigned int lev,TreeNode *r) { if(!r)return; if(lev+1>res.size()) { res.push_back(vector<int>()); } res[lev].push_back(r->val); zzloHelp(res,lev+1,r->left); zzloHelp(res,lev+1,r->right); } vector<vector<int>> zigzagLevelOrder(TreeNode* root) { vector<vector<int>> res; zzloHelp(res,0,root); for(unsigned int i=0;i<res.size();++i) { if(i%2==1) { int n=res[i].size(); for(int k=0;k<n/2;++k) swap(res[i][k],res[i][n-1-k]); } } return res; }
转载于:https://www.cnblogs.com/xLester/p/7570279.html