求给定二叉树的最大深度,
最大深度是指树的根结点到叶子结点
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型
*/
public int maxDepth (TreeNode root) {
if(root==null) return 0;
int leftmax=maxDepth(root.left);
int rightmax=maxDepth(root.right);
int max=leftmax>rightmax?leftmax+1:rightmax+1;
return max;
}
}