-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAIMETER.java
63 lines (51 loc) · 1.53 KB
/
DAIMETER.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
62
63
package BINARYTREE2;
public class DAIMETER {
static class Info{
int daim;
int ht;
public Info(int daim, int ht){
this.daim=daim;
this.ht=ht;
}
}
static class node{
int data;
node left;
node right;
node(int data){
this.data=data;
this.left=null;
this.right=null;
}
}
static class BinaryTree{
static int idx=-1;
public static node buildTree(int nodes[]){
idx++;
if(nodes[idx]==-1){
return null;
}
node newNode=new node(nodes[idx]);
newNode.left=buildTree(nodes);
newNode.right=buildTree(nodes);
return newNode;
}
}
public static Info diameter(node root){
if(root == null) { // Base case: if root is null, return 0 for both diameter and height
return new Info(0, 0);
}
Info rinfo=diameter(root.right);
Info linfo=diameter(root.left);
int diam=Math.max(linfo.daim, Math.max(rinfo.daim, linfo.ht+rinfo.ht+1));
int ht=Math.max(linfo.ht, rinfo.ht)+1;
return new Info(diam,ht);
}
public static void main(String args[]) {
int nodes[]={1,2,4,-1,-1,5,-1,-1,3,-1,6,-1,-1};
BinaryTree tree=new BinaryTree();
node root=tree.buildTree(nodes);
System.out.println(root.data);
System.out.println(diameter(root).daim);
}
}