二叉树最大深度(Java实现)
//计算树的最大深度
public int getDepth(){
return maxDepth(root);
}
private int maxDepth(Node node){
if (node==null){
return 0;
}
//当前树的最大深度
int max=0;
//左子树的最大深度
int maxL=0;
//右子树最大深度
int maxR=0;
if (node.left!=null){
maxL=maxDepth(node.left);
}
if (node.right!=null){
maxR=maxDepth(node.right);
}
max=maxL>=maxR?maxL+1:maxR+1;
return max;//返回本树最大深度
}