forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostordertraversal.java
More file actions
54 lines (41 loc) · 1 KB
/
Postordertraversal.java
File metadata and controls
54 lines (41 loc) · 1 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
# https://www.facebook.com/dikshit.kaushal.564/posts/103324861581146
# Subscribed By Dikshit Kaushal
class Node {
int item;
Node left, right;
public Node(int key) {
item = key;
left = right = null;
}
}
class Tree {
// Root of Binary Tree
Node root;
Tree() {
root = null;
}
void postorder(Node node) {
if (node == null)
return;
// traverse the left child
postorder(node.left);
// traverse the right child
postorder(node.right);
// traverse the root node
System.out.print(node.item + "->");
}
public static void main(String[] args) {
// create an object of Tree
Tree tree = new Tree();
// create nodes of the tree
tree.root = new Node(1);
tree.root.left = new Node(12);
tree.root.right = new Node(9);
// child nodes of left child
tree.root.left.left = new Node(5);
tree.root.left.right = new Node(6);
// postorder tree traversal
System.out.println("Postorder traversal");
tree.postorder(tree.root);
}
}