解题思路
使用递归算法来解决,注意递归的终止条件和递归的过程
// maxDepth函数用于返回当前子树的最大深度public int maxDepth(TreeNode root) {//定义递归的终止条件if(root == null)return 0;//递归地去求左子树和右子树的高度int leftDepth = maxDepth(root.left);int rightDepth = maxDepth(root.right);//取左右子树最大值 然后加上本层的层数return Math.max(leftDepth,rightDepth) + 1;}
