xxxxxxxxxx
/*
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));
}
}