Skip to content

Commit adfa3d3

Browse files
committed
0590 solved.
1 parent ad9ce2b commit adfa3d3

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-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_0590)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main2.cpp)
7+
add_executable(cpp_0590 ${SOURCE_FILES})
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// Source : https://leetcode.com/problems/n-ary-tree-postorder-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+
/// Recursion
26+
/// Time Complexity: O(n)
27+
/// Space Complexity: O(h)
28+
class Solution {
29+
public:
30+
vector<int> postorder(Node* root) {
31+
32+
vector<int> res;
33+
dfs(root, res);
34+
return res;
35+
}
36+
37+
private:
38+
void dfs(Node* node, vector<int>& res){
39+
40+
if(!node)
41+
return;
42+
43+
for(Node* next: node->children)
44+
dfs(next, res);
45+
res.push_back(node->val);
46+
}
47+
};
48+
49+
50+
int main() {
51+
52+
return 0;
53+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/// Source : https://leetcode.com/problems/n-ary-tree-postorder-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+
/// Non-Recursion
27+
/// Using stack
28+
///
29+
/// Time Complexity: O(n)
30+
/// Space Complexity: O(h)
31+
class Solution {
32+
public:
33+
vector<int> postorder(Node* root) {
34+
35+
vector<int> res;
36+
if(!root)
37+
return res;
38+
39+
stack<Node*> stack;
40+
stack.push(root);
41+
while(!stack.empty()){
42+
Node* cur = stack.top();
43+
stack.pop();
44+
res.push_back(cur->val);
45+
for(Node* next: cur->children)
46+
stack.push(next);
47+
}
48+
reverse(res.begin(), res.end());
49+
return res;
50+
}
51+
};
52+
53+
54+
int main() {
55+
56+
return 0;
57+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
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
| | | | | | |
382382
| 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+
| 590 | [N-ary Tree Postorder Transversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [C++](0590-N-ary-Tree-Postorder-Transversal/cpp-0590/) | | |
383384
| | | | | | |
384385
| 598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii/description/) | | [C++](0598-Range-Addition-II/cpp-0598/) | | |
385386
| 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/) | | |

0 commit comments

Comments
 (0)