/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ struct node{ int index; TreeNode *root; }; #include<queue> class Solution { public: /** * @param root TreeNode类 * @return int整型vector<vector<>> */ vector<vector<int> > zigzagLevelOrder(TreeNode* root) { // write code here vector<vector<int> > t; if(root==NULL)return t; queue <node> Q; vector<node> save; Q.push(node{0,root}); while(!Q.empty()) { node top=Q.front(); Q.pop(); save.push_back(top); if(top.root->left != NULL){Q.push(node{top.index+1,top.root->left});} if(top.root->right != NULL){Q.push(node{top.index+1,top.root->right});} } int num = save.size(); // cout<<num; vector<vector<int> > ans(save[num-1].index+1); for(int i = 0 ; i < num ; i++) { ans[save[i].index].push_back(save[i].root->val); } for(int i = 1 ; i < save[num-1].index+1 ; i+=2) { reverse(ans[i].begin(),ans[i].end()); } return ans; } };