Skip to content

Commit 9687c4b

Browse files
committed
0429 solved.
1 parent 8c41bc1 commit 9687c4b

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(cpp_0429)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main2.cpp)
7+
add_executable(cpp_0429 ${SOURCE_FILES})
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/// Source : https://leetcode.com/problems/n-ary-tree-level-order-traversal/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-10-30
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <queue>
8+
9+
using namespace std;
10+
11+
12+
/// Definition for a Node.
13+
class Node {
14+
public:
15+
int val = NULL;
16+
vector<Node*> children;
17+
18+
Node() {}
19+
20+
Node(int _val, vector<Node*> _children) {
21+
val = _val;
22+
children = _children;
23+
}
24+
};
25+
26+
27+
/// BFS
28+
/// Store step in the queue
29+
///
30+
/// Time Complexity: O(n)
31+
/// Space Complexity: O(n)
32+
class Solution {
33+
public:
34+
vector<vector<int>> levelOrder(Node* root) {
35+
36+
vector<vector<int>> res;
37+
if(!root)
38+
return res;
39+
40+
queue<pair<Node*, int>> q;
41+
q.push(make_pair(root, 0));
42+
while(!q.empty()){
43+
Node* cur = q.front().first;
44+
int step = q.front().second;
45+
q.pop();
46+
47+
if(step == res.size())
48+
res.push_back({cur->val});
49+
else
50+
res[step].push_back(cur->val);
51+
52+
for(Node* next: cur->children)
53+
q.push(make_pair(next, step + 1));
54+
}
55+
56+
return res;
57+
}
58+
};
59+
60+
61+
int main() {
62+
63+
return 0;
64+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// Source : https://leetcode.com/problems/n-ary-tree-level-order-traversal/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-10-30
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <queue>
8+
9+
using namespace std;
10+
11+
12+
/// Definition for a Node.
13+
class Node {
14+
public:
15+
int val = NULL;
16+
vector<Node*> children;
17+
18+
Node() {}
19+
20+
Node(int _val, vector<Node*> _children) {
21+
val = _val;
22+
children = _children;
23+
}
24+
};
25+
26+
27+
/// BFS, using Queue
28+
/// No need to store step in the queue :-)
29+
///
30+
/// Time Complexity: O(n)
31+
/// Space Complexity: O(n)
32+
class Solution {
33+
public:
34+
vector<vector<int>> levelOrder(Node* root) {
35+
36+
vector<vector<int>> res;
37+
if(!root)
38+
return res;
39+
40+
queue<Node*> q;
41+
q.push(root);
42+
while(!q.empty()){
43+
int n = q.size();
44+
vector<int> level;
45+
for(int i = 0; i < n; i ++){
46+
Node* node = q.front();
47+
q.pop();
48+
level.push_back(node->val);
49+
for(Node* next: node->children)
50+
q.push(next);
51+
}
52+
res.push_back(level);
53+
}
54+
55+
return res;
56+
}
57+
};
58+
59+
60+
int main() {
61+
62+
return 0;
63+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
326326
| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/description/) | [] | [C++](0416-Partition-Equal-Subset-Sum/cpp-0416/) | [Java](0416-Partition-Equal-Subset-Sum/java-0416/src/) | |
327327
| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | [] | | [Java](0417-Pacific-Atlantic-Water-Flow/java-0417/src/) | |
328328
| | | | | | |
329+
| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [] | [C++](0429-N-ary-Tree-Level-Order-Traversal/cpp-0429/) | | |
329330
| 430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/) | [] | [solution](0430-Flatten-a-Multilevel-Doubly-Linked-List/cpp-0430/) | | |
330331
| | | | | | |
331332
| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | | [C++](0434-Number-of-Segments-in-a-String/cpp-0434/) | | |

0 commit comments

Comments
 (0)