1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public: vector<vector<int>> levelOrder(TreeNode* root){ vector<vector<int>> ans; queue<TreeNode*> q; if(root) q.push(root);
while(!q.empty()){ int len = q.size(); vector<int> level; while(len --){ auto t = q.front(); q.pop(); level.push_back(t->val); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } ans.push_back(level); } return ans; } };
|