码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Maximum Depth of Binary Tree

时间:2015-07-14 17:15:29      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!