Skip to content

Commit 3a33a46

Browse files
committed
0589 solved.
1 parent 1042ea2 commit 3a33a46

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-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_0589)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main2.cpp)
7+
add_executable(cpp_0589 ${SOURCE_FILES})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/// Source : https://leetcode.com/problems/n-ary-tree-preorder-traversal/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-10-29
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Definition for a Node.
12+
class Node {
13+
public:
14+
int val;
15+
vector<Node*> children;
16+
17+
Node() {}
18+
19+
Node(int _val, vector<Node*> _children) {
20+
val = _val;
21+
children = _children;
22+
}
23+
};
24+
25+
26+
/// Recursion
27+
/// Time Complexity: O(n)
28+
/// Space Complexity: O(h)
29+
class Solution {
30+
public:
31+
vector<int> preorder(Node* root) {
32+
33+
vector<int> res;
34+
dfs(root, res);
35+
return res;
36+
}
37+
38+
private:
39+
void dfs(Node* node, vector<int>& res){
40+
41+
if(!node)
42+
return;
43+
44+
res.push_back(node->val);
45+
for(Node* next: node->children)
46+
dfs(next, res);
47+
}
48+
};
49+
50+
51+
int main() {
52+
53+
return 0;
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/// Source : https://leetcode.com/problems/n-ary-tree-preorder-traversal/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-10-29
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <stack>
8+
9+
using namespace std;
10+
11+
12+
/// Definition for a Node.
13+
class Node {
14+
public:
15+
int val;
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+
/// Non-Recursion
28+
/// Using stack
29+
/// Time Complexity: O(n)
30+
/// Space Complexity: O(h)
31+
class Solution {
32+
public:
33+
vector<int> preorder(Node* root) {
34+
35+
vector<int> res;
36+
if(!root)
37+
return res;
38+
39+
stack<pair<Node*, int>> stack;
40+
stack.push(make_pair(root, 1));
41+
while(!stack.empty()){
42+
Node* cur = stack.top().first;
43+
int step = stack.top().second;
44+
stack.pop();
45+
46+
res.push_back(cur->val);
47+
for(vector<Node*>::reverse_iterator iter = cur->children.rbegin();
48+
iter != cur->children.rend(); iter ++)
49+
stack.push(make_pair(*iter, step + 1));
50+
}
51+
return res;
52+
}
53+
};
54+
55+
56+
int main() {
57+
58+
return 0;
59+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
379379
| | | | | | |
380380
| 583 | [Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/description/) | [solution](https://leetcode.com/problems/delete-operation-for-two-strings/solution/) | [C++](0583-Delete-Operation-for-Two-Strings/cpp-0583/) | | |
381381
| | | | | | |
382+
| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [C++](0589-N-ary-Tree-Preorder-Traversal/cpp-0589/) | | |
383+
| | | | | | |
382384
| 598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii/description/) | | [C++](0598-Range-Addition-II/cpp-0598/) | | |
383385
| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/explore/learn/card/hash-table/184/comparison-with-other-data-structures/1177/) | [solution](https://leetcode.com/problems/minimum-index-sum-of-two-lists/solution/) | [C++](0599-Minimum-Index-Sum-of-Two-Lists/cpp-0599/) | | |
384386
| 600 | [Non-negative Integers without Consecutive Ones](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/description/) | [solution](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/solution/)<br/>[缺:Bit Manipulation]| [C++](0600-Non-negative-Integers-without-Consecutive-Ones/cpp-0600/) | | |

0 commit comments

Comments
 (0)