Repository navigation
Expand file tree
/
Copy pathNaryTreePostorderTraversal.java
More file actions
271 lines (191 loc) · 7.49 KB
/
Copy pathNaryTreePostorderTraversal.java
File metadata and controls
271 lines (191 loc) · 7.49 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package Algorithms.NAryTrees;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 16 October 2026
* @link 590. N-ary Tree Postorder Traversal <a href="https://leetcode.com/problems/n-ary-tree-postorder-traversal/">LeetCode link</a>
* @topics Stack, Tree, DFS
* @description Given the root of an n-ary tree, return the postorder traversal of its nodes' values.
* Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
Example 1:
Input: root = [1,null,3,2,4,null,5,6]
Output: [5,6,3,2,4,1]
Example 2:
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [2,6,7,11,14,3,8,12,4,9,13,10,5,1]
*/
public class NaryTreePostorderTraversal {
public static void main(String[] args) {
Integer[] nodes = {1,null,3,2,4,null,5,6};
Node root = Node.createNode(nodes);
System.out.printf("Given: %s%n", root);
System.out.printf("postorder Using RecursiveDfs: %s%n", postorderUsingRecursiveDfs(root));
System.out.printf("postorder Using IterativeDfs1: %s%n", postorderUsingIterativeDfs1(root));
System.out.printf("postorder Using IterativeDfs2: %s%n", postorderUsingIterativeDfs2(root));
System.out.printf("postorder Using IterativeDfs3: %s%n", postorderUsingIterativeDfs3(root));
System.out.printf("postorder Using IterativeDfs4: %s%n", postorderUsingIterativeDfs4(root));
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h) where h is the height of the tree
*/
public static List<Integer> postorderUsingRecursiveDfs(Node root) {
List<Integer> list = new ArrayList<>();
dfs(root, list);
return list;
}
private static void dfs(Node node, List<Integer> list) {
if (node == null) return;
for(Node child: node.children) {
dfs(child, list);
}
list.add(node.val);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h) where h is the height of the tree
*/
public static List<Integer> postorderUsingIterativeDfs1(Node root) {
List<Integer> list = new LinkedList<>();
Stack<Node> stack = new Stack<>();
if (root != null) stack.push(root);
while (!stack.isEmpty()) {
Node node = stack.pop();
list.addFirst(node.val);
int n = node.children.size();
for(Node child: node.children) {
stack.push(child);
}
}
return list;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h) where h is the height of the tree
*/
public static List<Integer> postorderUsingIterativeDfs2(Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<Node> stack = new Stack<>();
stack.add(root);
while (!stack.isEmpty()) {
Node node = stack.pop();
result.add(node.val);
for (Node child : node.children) stack.add(child);
}
Collections.reverse(result);
return result;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h) where h is the height of the tree
*/
public static List<Integer> postorderUsingIterativeDfs3(Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<Node> nodeStack = new Stack<>(); // Stack for traversal
Stack<Node> reverseStack = new Stack<>(); // Stack to reverse the order
nodeStack.push(root);
while (!nodeStack.isEmpty()) {
Node node = nodeStack.pop();
reverseStack.push(node);
for (Node child : node.children) {
nodeStack.push(child);
}
}
while (!reverseStack.isEmpty()) {
Node node = reverseStack.pop();
result.add(node.val);
}
return result;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h) where h is the height of the tree
*/
public static List<Integer> postorderUsingIterativeDfs4(Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<NodeVisitPair> nodeStack = new Stack<>();
nodeStack.push(new NodeVisitPair(root, false));
while (!nodeStack.isEmpty()) {
NodeVisitPair currentPair = nodeStack.pop();
if (currentPair.isVisited) {
result.add(currentPair.node.val); // If the node has been visited, add its value to the result
} else { // Mark the current node as visited and push it back to the stack
currentPair.isVisited = true;
nodeStack.push(currentPair);
// Push all children to the stack in reverse order
List<Node> children = currentPair.node.children;
for (int i = children.size() - 1; i >= 0; i--) {
nodeStack.push(new NodeVisitPair(children.get(i), false));
}
}
}
return result;
}
private static class NodeVisitPair {
Node node;
boolean isVisited;
NodeVisitPair(Node node, boolean isVisited) {
this.node = node;
this.isVisited = isVisited;
}
}
//region Node
public static class Node {
public int val;
public List<Node> children;
public Node() {
children = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
children = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _children) {
val = _val;
children = _children;
}
/*
Nodes: [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
1
_____________|___________
/ / \ \
2 3 4 5
/ \ | / \
6 7 8 9 10
| | |
11 12 13
|
14
*/
public static Node createNode(Integer[] nodes) {
if (nodes == null || nodes.length == 0 || nodes[0] == null) {
return null;
}
Node root = new Node(nodes[0]);
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
int i = 2; // Index 0 is root, Index 1 is the first null delimiter
while (i < nodes.length && !queue.isEmpty()) {
Node parent = queue.poll();
// Collect all children for the current parent until reaching null or array end
while (i < nodes.length && nodes[i] != null) {
Node child = new Node(nodes[i]);
parent.children.add(child);
queue.offer(child);
i++;
}
i++; // Move past the null delimiter that marks the end of this parent's child list
}
return root;
}
@Override
public String toString() {
if (children == null || children.isEmpty()) return "{" + val + "}";
return "{" + val + ", children=" + children + "}";
}
}
//endregion
}