“ Ctrl AC!一起 AC!”
题目:原题
思路:
树的广度优先搜索
代码:
/*
// Definition for a Node.
class Node {
public:
int val;
vector children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector> levelOrder(Node* root) {
if(root==NULL) return{};
vector> ans;
queue q;
q.push(root);
while(!q.empty()){
vector level;
int cnt=q.size();
for(int i=0;ival);
for(int i=0;ichildren.size();i++){
q.push(temp->children[i]);
}
}
ans.push_back(level);
}
return ans;
}
};
感谢阅读!!!
“ Ctrl AC!一起 AC!”
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)