/
PostorderTraversal.java
93 lines (83 loc) · 2.77 KB
/
PostorderTraversal.java
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
package org.redquark.techinterview.dsa.binarytree;
import java.util.Stack;
/**
* @author Anirudh Sharma
* * <p>
* * Given a binary tree, write an iterative and recursive solution to traverse the tree using postorder traversal.
*/
public class PostorderTraversal {
private static void postorderTraversalRecursively(Node root) {
if (root == null) {
return;
}
postorderTraversalRecursively(root.left);
postorderTraversalRecursively(root.right);
System.out.print(root.data + " ");
}
private static void postorderTraversalIteratively(Node root) {
if (root == null) {
return;
}
// Stack to store the nodes of the tree
Stack<Node> nodes = new Stack<>();
// Push the root to the stack
nodes.push(root);
// Another stack to store postorder traversal
Stack<Integer> postOrder = new Stack<>();
// Loop until the stack is empty
while (!nodes.isEmpty()) {
// Get the current node from the stack
Node current = nodes.pop();
postOrder.push(current.data);
// Push left child if not null
if (current.left != null) {
nodes.add(current.left);
}
// Push right child if not null
if (current.right != null) {
nodes.add(current.right);
}
}
while (!postOrder.isEmpty()) {
System.out.print(postOrder.pop() + " ");
}
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(3);
root.right = new Node(2);
System.out.println("Recursive:");
postorderTraversalRecursively(root);
System.out.println("\nIterative:");
postorderTraversalIteratively(root);
System.out.println("\n");
root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(60);
System.out.println("Recursive:");
postorderTraversalRecursively(root);
System.out.println("\nIterative:");
postorderTraversalIteratively(root);
System.out.println("\n");
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
System.out.println("Recursive:");
postorderTraversalRecursively(root);
System.out.println("\nIterative:");
postorderTraversalIteratively(root);
System.out.println("\n");
}
static class Node {
final int data;
Node left;
Node right;
Node(int data) {
this.data = data;
}
}
}