c_590_N_ary_Tree_Postorder_Traversal

//
// Created by Mr.Hu on 2018/12/24.
//
// leetcode 590 n ary tree postorder traversal
//
// 题目要求对N叉树进行后序遍历
//
// 这里使用递归的方式进行遍历,类似于二叉树的后序遍历,只是在处理节点上,需要先循环访问节点的孩子节点,再访问当前节点。
// 使用循环的方式进行实现同样需要借助栈(stack)来进行,这里不再重复。
// 这里循环的方式和先序有一点不同,在于对节点进行访问后,每次访问孩子节点后,
// 需要将节点的孩子节点置为nullptr(具体操作应该是将vector<Node*> children进行pop清空操作)
//

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <vector>
using namespace std;

class Node {
public:
int val;
vector<Node *> children;
Node() {}
Node(int _val, vector<Node *> _children) {
val = _val;
children = _children;
}
};

class Solution {
public:
vector<int> postorder(Node *root) {
vector<int> result;
postorder_traversal(root, result);
return result;
}

void postorder_traversal(Node *node, vector<int> &result) {
if (!node) {
return;
}
vector<Node *> children = node->children;
for (auto child:children) {
postorder_traversal(child, result);
}
result.push_back(node->val);
}
};

int main() {
vector<Node *> children1;
Node e, f;
e.val = 5;
f.val = 6;
children1.push_back(&e);
children1.push_back(&f);

vector<Node *> children2;
Node b, c, d;
b.val = 3;
c.val = 2;
d.val = 4;
b.children = children1;
children2.push_back(&b);
children2.push_back(&c);
children2.push_back(&d);

Node a;
a.val = 1;
a.children = children2;

Solution solution;
vector<int> result = solution.postorder(&a);

for (auto i:result) {
cout << i << " ";
}
return 0;
}