题目描述:
https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/
代码:
class Solution {
private:
vector<vector<int> > ans;
vector<int> anss;
queue<Node *> myqueue;
public:
vector<vector<int>> levelOrder(Node* root) {
if(!root)
return ans;
myqueue.push(root);
while(!myqueue.empty())
{
anss.erase(anss.begin(), anss.end());
int size = myqueue.size();
for(int i=0; i<size; i++)
{
Node * temp = myqueue.front();
myqueue.pop();
anss.push_back(temp->val);
if(!temp->children.empty())
{
for(Node* child : temp->children)
myqueue.push(child);
}
}
ans.push_back(anss);
}
return ans;
}
};
网友评论