题目:
分析:
水题,为什么还是中等。
代码:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
queue<Node*> q;
q.push(root);
vector<vector<int>> ans;
if(root==NULL) return ans;
while(!q.empty())
{
vector<int> v;
queue<Node*> qq;
while(!q.empty())
{
Node* nn = q.front();
q.pop();
v.push_back(nn->val);
vector<Node*> v2 = nn->children;
//cout<<"v2.size()-1 :"<<v2.size()<<endl;
for(int i=0;i<v2.size();i++){
qq.push(v2[i]);
}
//cout<<'2'<<endl;
}
//cout<<'1'<<endl;
q=qq;
ans.push_back(v);
}
return ans;
}
};