题目:https://leetcode-cn.com/explore/learn/card/data-structure-binary-tree/2/traverse-a-tree/9/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
if(!root) return ans;
queue<TreeNode*> q;
q.push(root);
TreeNode* tr;
while(!q.empty()){
int n = q.size();
vector<int> v;
for(int i = 0;i<n;i++){
tr = q.front();
v.push_back(tr->val);
q.pop();
if(tr->left) q.push(tr->left);
if(tr->right) q.push(tr->right);
}
ans.push_back(v);
}
return ans;
}
};