/*
Following is the structure used to represent the Binary Tree Node
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
public BinaryTreeNode(T data) {
this.data = data;
this.left = null;
this.right = null;
}
}
*/
class Pair {
int ht;
int diameter;
}
public class Solution {
public static int diameterOfBinaryTree( BinaryTreeNode< Integer> root) {
Pair ans = diameterOfBinaryTree_( root) ;
return ans.diameter ;
}
public static Pair diameterOfBinaryTree_( BinaryTreeNode< Integer> root) {
if ( root == null ) {
Pair base = new Pair( ) ;
base.ht = 0 ;
base.diameter = 0 ;
return base;
}
Pair left = diameterOfBinaryTree_( root.left ) ;
Pair right = diameterOfBinaryTree_( root.right ) ;
Pair myAns = new Pair( ) ;
myAns.
ht = Math .
max ( left.
ht , right.
ht ) + 1 ;
int opt1 = left.diameter ;
int opt2 = right.diameter ;
int opt3 = left.ht + right.ht + 1 ;
myAns.
diameter = Math .
max ( opt1,
Math .
max ( opt2, opt3
) ) ;
return myAns;
}
}
LyoKCQoJRm9sbG93aW5nIGlzIHRoZSBzdHJ1Y3R1cmUgdXNlZCB0byByZXByZXNlbnQgdGhlIEJpbmFyeSBUcmVlIE5vZGUKCgljbGFzcyBCaW5hcnlUcmVlTm9kZTxUPiB7CgkJVCBkYXRhOwoJCUJpbmFyeVRyZWVOb2RlPFQ+IGxlZnQ7CgkJQmluYXJ5VHJlZU5vZGU8VD4gcmlnaHQ7CgoJCXB1YmxpYyBCaW5hcnlUcmVlTm9kZShUIGRhdGEpIHsKCQkJdGhpcy5kYXRhID0gZGF0YTsKCQkJdGhpcy5sZWZ0ID0gbnVsbDsKCQkJdGhpcy5yaWdodCA9IG51bGw7CgkJfQoJfQoKKi8KCgpjbGFzcyBQYWlyIHsKCglpbnQgaHQ7CglpbnQgZGlhbWV0ZXI7Cn0KCgpwdWJsaWMgY2xhc3MgU29sdXRpb24gewoKCXB1YmxpYyBzdGF0aWMgaW50IGRpYW1ldGVyT2ZCaW5hcnlUcmVlKEJpbmFyeVRyZWVOb2RlPEludGVnZXI+IHJvb3QpewoJCQoJCVBhaXIgYW5zID0gZGlhbWV0ZXJPZkJpbmFyeVRyZWVfKHJvb3QpOwoKCQlyZXR1cm4gYW5zLmRpYW1ldGVyOwoJfQoKCglwdWJsaWMgc3RhdGljIFBhaXIgZGlhbWV0ZXJPZkJpbmFyeVRyZWVfKEJpbmFyeVRyZWVOb2RlPEludGVnZXI+IHJvb3QpewoJCQoJCWlmIChyb290ID09IG51bGwpIHsKCQkJUGFpciBiYXNlID0gbmV3IFBhaXIoKTsKCQkJCgkJCWJhc2UuaHQgPSAwOwoJCQliYXNlLmRpYW1ldGVyID0gMDsKCgkJCXJldHVybiBiYXNlOwoJCX0KCgoJCVBhaXIgbGVmdCA9IGRpYW1ldGVyT2ZCaW5hcnlUcmVlXyhyb290LmxlZnQpOwoJCVBhaXIgcmlnaHQgPSBkaWFtZXRlck9mQmluYXJ5VHJlZV8ocm9vdC5yaWdodCk7CgoJCVBhaXIgbXlBbnMgPSBuZXcgUGFpcigpOwoKCQlteUFucy5odCA9IE1hdGgubWF4KGxlZnQuaHQsIHJpZ2h0Lmh0KSArIDE7CgoJCWludCBvcHQxID0gbGVmdC5kaWFtZXRlcjsKCgkJaW50IG9wdDIgPSByaWdodC5kaWFtZXRlcjsKCgkJaW50IG9wdDMgPSBsZWZ0Lmh0ICsgcmlnaHQuaHQgKyAxOwoKCQlteUFucy5kaWFtZXRlciA9IE1hdGgubWF4KG9wdDEsIE1hdGgubWF4KG9wdDIsIG9wdDMpKTsKCgkJcmV0dXJuIG15QW5zOwoJfQoKCgkKfQ==
compilation info
Main.java:27: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
^
Main.java:29: error: cannot find symbol
public static int diameterOfBinaryTree(BinaryTreeNode<Integer> root){
^
symbol: class BinaryTreeNode
location: class Solution
Main.java:37: error: cannot find symbol
public static Pair diameterOfBinaryTree_(BinaryTreeNode<Integer> root){
^
symbol: class BinaryTreeNode
location: class Solution
3 errors
stdout