/*
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;
}
}
*/
public class Solution {
public static int height( BinaryTreeNode< Integer> node) {
if ( node == null )
return 0 ;
return Math .
max ( height
( node.
left ) , height
( node.
right ) ) + 1 ; }
public static int diameterOfBinaryTree( BinaryTreeNode< Integer> root) {
if ( root == null )
return 0 ;
int opt1 = height( root.left ) + height( root.right ) + 1 ;
int opt2 = diameterOfBinaryTree( root.left ) ;
int opt3 = diameterOfBinaryTree( root.right ) ;
return Math .
max ( opt1,
Math .
max ( opt2, opt3
) ) ; }
}
LyoKCQoJRm9sbG93aW5nIGlzIHRoZSBzdHJ1Y3R1cmUgdXNlZCB0byByZXByZXNlbnQgdGhlIEJpbmFyeSBUcmVlIE5vZGUKCgljbGFzcyBCaW5hcnlUcmVlTm9kZTxUPiB7CgkJVCBkYXRhOwoJCUJpbmFyeVRyZWVOb2RlPFQ+IGxlZnQ7CgkJQmluYXJ5VHJlZU5vZGU8VD4gcmlnaHQ7CgoJCXB1YmxpYyBCaW5hcnlUcmVlTm9kZShUIGRhdGEpIHsKCQkJdGhpcy5kYXRhID0gZGF0YTsKCQkJdGhpcy5sZWZ0ID0gbnVsbDsKCQkJdGhpcy5yaWdodCA9IG51bGw7CgkJfQoJfQoKKi8KCnB1YmxpYyBjbGFzcyBTb2x1dGlvbiB7CgoJcHVibGljIHN0YXRpYyBpbnQgaGVpZ2h0KEJpbmFyeVRyZWVOb2RlPEludGVnZXI+IG5vZGUpIHsKCgkJaWYgKG5vZGUgPT0gbnVsbCkKCQkJcmV0dXJuIDA7CgoJCXJldHVybiBNYXRoLm1heChoZWlnaHQobm9kZS5sZWZ0KSwgaGVpZ2h0KG5vZGUucmlnaHQpKSArIDE7Cgl9CgoKCXB1YmxpYyBzdGF0aWMgaW50IGRpYW1ldGVyT2ZCaW5hcnlUcmVlKEJpbmFyeVRyZWVOb2RlPEludGVnZXI+IHJvb3QpewoKCQlpZiAocm9vdCA9PSBudWxsKQoJCQlyZXR1cm4gMDsKCgkJaW50IG9wdDEgPSBoZWlnaHQocm9vdC5sZWZ0KSArIGhlaWdodChyb290LnJpZ2h0KSArIDE7CgoJCWludCBvcHQyID0gZGlhbWV0ZXJPZkJpbmFyeVRyZWUocm9vdC5sZWZ0KTsKCgkJaW50IG9wdDMgPSBkaWFtZXRlck9mQmluYXJ5VHJlZShyb290LnJpZ2h0KTsKCgkJcmV0dXJuIE1hdGgubWF4KG9wdDEsIE1hdGgubWF4KG9wdDIsIG9wdDMpKTsKCX0KCQp9
compilation info
Main.java:19: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
^
Main.java:21: error: cannot find symbol
public static int height(BinaryTreeNode<Integer> node) {
^
symbol: class BinaryTreeNode
location: class Solution
Main.java:30: error: cannot find symbol
public static int diameterOfBinaryTree(BinaryTreeNode<Integer> root){
^
symbol: class BinaryTreeNode
location: class Solution
3 errors
stdout