-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterativeInorder.java
51 lines (46 loc) · 1.37 KB
/
iterativeInorder.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
package striver.Treee;
import java.util.*;
class TreeNode{
int data;
TreeNode left;
TreeNode right;
TreeNode(int data){
this.data = data;
left = null;
right = null;
}
}
public class iterativeInorder {
public static List<Integer> inorderrTrav(TreeNode root){
List<Integer> ans = new ArrayList<>(0);
Stack<TreeNode> st = new Stack<TreeNode>();
TreeNode node = root;
while(true){
if(node != null){
st.push(node);
node = node.left;
}
else{
if(st.empty()){
break;
}
node = st.pop();
ans.add(node.data);
node = node.right;
}
}
return ans;
}
public static void main(String[]args){
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.right.left = new TreeNode(4);
root.right.right = new TreeNode(5);
root.right.left.left = new TreeNode(7);
root.left.left = new TreeNode(8);
root.left.right = new TreeNode(9);
List<Integer> iterativeInOrderElm = inorderrTrav(root);
System.out.println(" the iterative elm is : " + inorderrTrav(root));
}
}