给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
返回其层序遍历:
[
[1],
[3,2,4],
[5,6]
]
说明:
(1)树的深度不会超过 1000。
(2)树的节点总数不会超过 5000。
/*
// 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) {
vector<vector<int>>vv;
if(root==NULL)
return vv;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
vector<int> tmp;
int len=q.size();
for(int i=0;i<len;i++)
{
Node* t=q.front();
q.pop();
tmp.push_back(t->val);
for(int j=0;j<t->children.size();j++)
{
q.push(t->children[j]);
}
}
vv.push_back(tmp);
}
return vv;
}
};