Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
递归思路,左右叶子记录各自的深度,返回左右叶子的最大深度+1即可。
压栈思路:先左节点全入,然后左出右入。
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int leftLeafDepth=maxDepth(root.left);
int rightLeafDepth=maxDepth(root.right);
int result = Math.max(leftLeafDepth, rightLeafDepth)+1;
return result;
}