FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_03/id_102/leetcode_429_102.cpp at master · augfool/algorithm · GitHub
augfool
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_03
/
id_102
/
leetcode_429_102.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (44 loc) · 1.17 KB
Breadcrumbs
algorithm
/
Week_03
/
id_102
/
leetcode_429_102.cpp
Copy path
File metadata and controls
48 lines (44 loc) · 1.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
// 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) {
/*
首先判断root是否为空,为空直接返回空数组
*/
if
(root ==
NULL
) {
return
{};
}
vector<vector<
int
>> res;
queue<Node*> q;
q.
push
(root);
while
(!q.
empty
()) {
/*
用level定义每层的数组
*/
vector<
int
> level;
for
(
int
i = q.
size
(); i >
0
; --i) {
/*
对t的类型进行自动推导
*/
auto
t = q.
front
();
q.
pop
();
level.
push_back
(t->
val
);
/*
子节点放入children数组
*/
if
(!t->
children
.
empty
()) {
/*
对t->children进行遍历
*/
for
(
auto
a : t->
children
) {
q.
push
(a);
}
}
}
res.
push_back
(level);
}
return
res;
}
};
Back
|
FazBrowse Home
|
New Git URL