给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
deque<Node*> d;
vector<vector<int>> res;
if(!root)
return res;
d.push_back(root);
while(!d.empty())
{
vector<int >tmp;
for(auto i: d)
tmp.push_back(i->val);
int len=d.size();
while(len--)
{
if(d[0]->children.empty())
d.pop_front();
else
{
for(auto i: d[0]->children)
d.push_back(i);
d.pop_front();
}
}
res.push_back(tmp);
}
return res;
}
};