题目:
思路:最后一层(空层)返回0;然后上面层为其左右结点层数+1;递归; (dfs?)
代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
int l=maxDepth(root.left);
int r=maxDepth(root.right);
return (l>r? l:r)+1;
}
}