求二叉树的最大距离
输入图例:
代码:
static int max=0;
static int maxDepth(BinaryTreeNode root) {
if (root == null) {
return 0;
}
int maxLeft = (root.getLeft() == null) ? 0 : 1 + maxDepth(root.getLeft());
int maxRight = (root.getRight() == null) ? 0 : 1 + maxDepth(root.getRight());
int tempMax=maxLeft+maxRight;
max=(tempMax>max)?tempMax:max;
return (maxLeft>maxRight)?maxLeft:maxRight;
}
最后max的值为: