Skip to content

Commit d38654c

Browse files
committed
Tree traversals 2
1 parent 45650e4 commit d38654c

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed
Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
11
package tree_traversals;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
import tree_implementations.Node;
8+
39
public class Postorder_iterative_using_1_stack {
410

5-
public static void main(String[] args) {
11+
public static List<Integer> postorderTraversal(Node root) {
12+
List<Integer> postorder = new ArrayList<Integer>();
613

14+
return postorder;
15+
}
16+
17+
static Scanner sc = null;
18+
19+
public static void main(String[] args) {
20+
21+
sc = new Scanner(System.in);
22+
23+
Node root = createTree();
24+
25+
List<Integer> ans = postorderTraversal(root);
26+
System.out.print("Post order: ");
27+
for (int data : ans) {
28+
System.out.print(data + ", ");
29+
}
30+
31+
}
32+
33+
static Node createTree() {
34+
35+
Node root = null;
36+
System.out.println("Enter data: ");
37+
int data = sc.nextInt();
38+
39+
if (data == -1)
40+
return null;
41+
42+
root = new Node(data);
43+
44+
System.out.println("Enter left for " + data);
45+
root.left = createTree();
46+
47+
System.out.println("Enter right for " + data);
48+
root.right = createTree();
49+
50+
return root;
751
}
852

953
}
54+
55+
// Post order: 4, 5, 2, 8, 7, 6, 3, 1,
Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,83 @@
11
package tree_traversals;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
import java.util.Stack;
7+
8+
import tree_implementations.Node;
9+
10+
/*
11+
* Time Complexity: O(N)
12+
* where N is the no. of nodes in the tree
13+
* Space Complexity: O(2N) (size due to usage of 2 stacks)
14+
* where N is the no. of nodes in the tree
15+
* (when queue stores every node - worst case)
16+
* (don't take the space of answer DS into account)
17+
*/
318
public class Postorder_iterative_using_2_stacks {
419

20+
public static List<Integer> postorderTraversal(Node root) {
21+
List<Integer> postorder = new ArrayList<Integer>();
22+
Stack<Node> stack1 = new Stack<Node>();
23+
Stack<Node> stack2 = new Stack<Node>();
24+
25+
if (root == null)
26+
return postorder;
27+
28+
stack1.push(root);
29+
while (!stack1.isEmpty()) {
30+
root = stack1.pop();
31+
stack2.push(root);
32+
if (root.left != null)
33+
stack1.push(root.left);
34+
if (root.right != null)
35+
stack1.push(root.right);
36+
}
37+
38+
while (!stack2.isEmpty()) {
39+
postorder.add(stack2.pop().data);
40+
}
41+
42+
return postorder;
43+
}
44+
45+
static Scanner sc = null;
46+
547
public static void main(String[] args) {
6-
48+
49+
sc = new Scanner(System.in);
50+
51+
Node root = createTree();
52+
53+
List<Integer> ans = postorderTraversal(root);
54+
System.out.print("Post order: ");
55+
for (int data : ans) {
56+
System.out.print(data + ", ");
57+
}
58+
59+
}
60+
61+
static Node createTree() {
62+
63+
Node root = null;
64+
System.out.println("Enter data: ");
65+
int data = sc.nextInt();
66+
67+
if (data == -1)
68+
return null;
69+
70+
root = new Node(data);
71+
72+
System.out.println("Enter left for " + data);
73+
root.left = createTree();
74+
75+
System.out.println("Enter right for " + data);
76+
root.right = createTree();
77+
78+
return root;
779
}
880

981
}
82+
83+
// Post order: 4, 5, 2, 8, 7, 6, 3, 1,

0 commit comments

Comments
 (0)