class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
Node* t;
queue<Node*>q;
vector<int >a;
int size = 1;
vector<vector<int>>ans;
if (root)q.push(root);
while (!q.empty()) {
t = q.front();
a.push_back(t->val);
q.pop();
for (int i = 0; i < t->children.size(); i++)
q.push(t->children[i]);
if (a.size() == size)
ans.push_back(a), a.clear(), size = q.size();
}
return ans;
}
};
LeetCode 429. N 叉树的层序遍历
最新推荐文章于 2024-07-13 19:27:27 发布