Question:
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、题型分类:
2、思路:
递归实现基本思想:
为了求得树的深度,可以先求左右子树的深度,取二者较大者加1即是树的深度,递归返回的条件是若节点为空,返回0
3、时间复杂度:
4、代码:
public int maxDepth(TreeNode root) { if(root==null) return 0;//递归终止 return 1+Math.max(maxDepth(root.left), maxDepth(root.right)); }
5、优化:
非递归的实现
6、扩展:
[LeetCode] Maximum Depth of Binary Tree
原文:http://www.cnblogs.com/maydow/p/4645450.html