简单题
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<vector<int> > levelOrder(TreeNode *root) { vector<vector<int> > ans; if(root == nullptr) return ans; queue<TreeNode* > que[2]; int mark = 0; que[0].push(root); while(!que[mark].empty()) { int next = (mark + 1) % 2; vector<int> level; while(!que[mark].empty()) { TreeNode* tmp = que[mark].front() ; que[mark].pop(); level.push_back(tmp -> val); if(tmp -> left) que[next].push(tmp -> left); if(tmp -> right) que[next].push(tmp -> right); } ans.push_back(level); mark = next; } return ans; } };