class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res;
if (root == NULL)
return res;
queue<Node*> q;
int cur_len = 1;
int next_len = 0;
int i;
q.push(root);
while (!q.empty())
{
vector<int> tmp;
while(cur_len > 0)
{
Node* tmp_node = q.front();
q.pop();
tmp.push_back(tmp_node->val);
for (i=0; i<tmp_node->children.size();i++)
{
q.push(tmp_node->children[i]);
next_len++;
}
cur_len--;
}
res.push_back(tmp);
cur_len = next_len;
next_len = 0;
}
return res;
}
};
leetcode N叉树按层遍历
最新推荐文章于 2022-09-09 22:47:26 发布