/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { if(!pRoot) return {}; vector<vector<int>> res; queue<TreeNode*> que; que.push(pRoot); int level = 1; while(!que.empty()) { int size = que.size(); vector<int> vec; // 每一层节点的处理 for(int i=0; i<size; i++) { TreeNode* node = que.front(); que.pop(); vec.push_back(node->val); if(node->left) que.push(node->left); if(node->right) que.push(node->right); } // 奇偶层的判断,是否反转 if(level%2 == 0) reverse(vec.begin(), vec.end()); level ++; // 收集结果 res.push_back(vec); } return res; } };