-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeTraversal.java
More file actions
94 lines (79 loc) · 2.44 KB
/
Copy pathTreeTraversal.java
File metadata and controls
94 lines (79 loc) · 2.44 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
import java.lang.Math;
import java.util.Stack;
class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public class Main {
public static void preorderTraversal(Node root) {
if(root == null)
return;
Stack<Node> stack = new Stack<Node>();
stack.add(root);
while(!stack.empty()) {
Node current = stack.pop();
System.out.print(current.data+", ");
if(current.right != null)
stack.add(current.right);
if(current.left != null)
stack.add(current.left);
}
}
public static void inorderTraversal(Node root) {
if(root == null)
return;
Stack<Node> stack = new Stack<Node>();
Node current = root;
while(!stack.empty() || current != null) {
if(current != null) {
stack.add(current);
current = current.left;
} else {
current = stack.pop();
System.out.print(current.data+", ");
current = current.right;
}
}
}
public static void postorderTraversal(Node root) {
if(root == null)
return;
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
s1.add(root);
while(!s1.empty()) {
Node current = s1.pop();
if(current.left != null)
s1.add(current.left);
if(current.right != null)
s1.add(current.right);
s2.add(current);
}
while(!s2.empty()) {
Node current = s2.pop();
System.out.print(current.data+", ");
}
}
public static void main(String[] args) {
Node root = new Node(5);
root.left = new Node(2);
root.left.left = new Node(0);
root.left.right = new Node(3);
root.left.left.right = new Node(1);
root.right = new Node(6);
root.right.right = new Node(9);
root.right.right.left = new Node(7);
root.right.right.left.right = new Node(8);
inorderTraversal(root);
System.out.println();
preorderTraversal(root);
System.out.println();
postorderTraversal(root);
}
}