-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatTree.java
More file actions
50 lines (39 loc) · 1.14 KB
/
FlatTree.java
File metadata and controls
50 lines (39 loc) · 1.14 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
import java.util.ArrayList;
public class FlatTree {
static ArrayList<String> tree = new ArrayList<String>();
static class BinaryTree{
BinaryTree left;
BinaryTree right;
String s;
BinaryTree(BinaryTree l, BinaryTree r, String payload){
left = l;
right = r;
s = payload;
}
}
static void flatten (BinaryTree root){
if(root == null) return;
if(root.left != null ) flatten(root.left);
tree.add(root.s);
if(root.right != null ) flatten(root.right);
}
public static void main(String[] args){
BinaryTree node0 = new BinaryTree(null, null, "Google");
BinaryTree node1 = new BinaryTree(null, null, "CodeU");
BinaryTree node2 = new BinaryTree(null, null, "is");
BinaryTree node3 = new BinaryTree(null, null, "awesome");
BinaryTree node4 = new BinaryTree(null, null, "woot woot");
BinaryTree node5 = new BinaryTree(null, null, "summertime");
BinaryTree node6 = new BinaryTree(null, null, "Dhrumil");
node0.left = node1;
node0.right = node2;
node1.left = node3;
node1.right = node4;
node2.left = node5;
node2.right = node6;
flatten(node0);
for(String a : tree){
System.out.println(a);
}
}
}