递归计算二叉树深度,递归左深度和右深度的值取最大,并加上初始层,即加一
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
else{
int ll = maxDepth(root.left);
int rl = maxDepth(root.right);
return Math.max(ll,rl) + 1;
}
}
}