【解题思路】
一个节点的深度等于它的左子树的最大深度和右子树的最大深度的最大值,再加上1。
maxDepth(node) = Math.max(maxDepth(node.left), maxDepth(node.right)) +1。
/**
* 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) {
return depth(root);
}
public int depth(TreeNode node)
{
if(node == null) return 0;
else return Math.max(depth(node.left), depth(node.right))+1;
}
}