/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return int整型vector<vector<>> */ vector<vector<int> > Print(TreeNode* pRoot) { // write code here vector<vector<int>> res; if(pRoot==nullptr) return {}; queue<TreeNode*> q; q.push(pRoot); int dep=1; while(!q.empty()) { int l=q.size(); vector<int> cur; dep++; for(int i=0;i<l;i++) { TreeNode* node = q.front(); q.pop(); cur.push_back(node->val); if(node->left) q.push(node->left); if(node->right) q.push(node->right); } if(dep%2==1) reverse(cur.begin(),cur.end()); res.push_back(cur); } return res; } };