-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
41 lines (34 loc) · 930 Bytes
/
BinaryTree.java
File metadata and controls
41 lines (34 loc) · 930 Bytes
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
public class BinaryTree {
public Node gate;
public BinaryTree left, right;
public BinaryTree(Node gateType, BinaryTree t1 , BinaryTree t2 ){
this.gate = gateType;
left = t1;
right = t2;
}
//This is pre-order traversal
public static String printTree(BinaryTree t){
if(t == null){
return "";
}else{
return t.gate.getName() + printTree(t.left) + printTree(t.right);
}
}
public static int maxNum(int num1, int num2){
if(num2 > num1){
return num2;
}else{
return num1;
}
}
public static int maxLength(BinaryTree t){
if(t==null){
return 0;
}else{
return BinaryTree.maxNum(maxLength(t.left), maxLength(t.right)) + 1;
}
}
public static String printRight(BinaryTree t){
return printTree(t.right);
}
}