-
Notifications
You must be signed in to change notification settings - Fork 0
/
CN Tree Traversals (in, pre, post combined) - iterative.java
61 lines (55 loc) · 1.76 KB
/
CN Tree Traversals (in, pre, post combined) - iterative.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
import java.util.* ;
import java.io.*;
/************************************************************
Following is the Binary Tree node structure:
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
public BinaryTreeNode(T data) {
this.data = data;
}
}
************************************************************/
class Element {
BinaryTreeNode<Integer> node;
int num;
Element(BinaryTreeNode<Integer> node, int num) {
this.node = node;
this.num = num;
}
}
public class Solution {
public static List<List<Integer>> getTreeTraversal(BinaryTreeNode<Integer> root) {
// Write your code here.
if (root == null) return new ArrayList<>();
Stack<Element> stk = new Stack<>();
List<Integer> pre = new ArrayList<>();
List<Integer> in = new ArrayList<>();
List<Integer> post = new ArrayList<>();
stk.push(new Element(root, 1));
while (!stk.isEmpty()) {
Element e = stk.pop();
BinaryTreeNode<Integer> cur = e.node;
int num = e.num;
if (num == 1) {
pre.add(cur.data);
stk.push(new Element(cur, 2));
if (cur.left != null) stk.push(new Element(cur.left, 1));
}
else if (num == 2) {
in.add(cur.data);
stk.push(new Element(cur, 3));
if (cur.right != null) stk.push(new Element(cur.right, 1));
}
else if (num == 3){
post.add(cur.data);
}
}
List<List<Integer>> ans = new ArrayList<>();
ans.add(in);
ans.add(pre);
ans.add(post);
return ans;
}
}