tree_diameter

发布时间 2023-05-31 20:33:19作者: 哎呦_不想学习哟~

 

 

 

 

public static int height(BinTree T) {
if (T == null) {
return -1;
} else {
return Math.max(height(T.left), height(T.right)) + 1;
}
}
/** Return the diameter of T. */
public static int diameter(BinTree T) {
if (T == null ) {
return 0;
} else {
int childDiam = Math.max(diameter(T.left), diameter(T.right));
int nodeDiam = 2 + height(T.left) + height(T.right);
return Math.max(childDiam, nodeDiam);
}