题目:

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的距离。

样例

给出一棵如下的二叉树:

  1
/ \
2 3
/ \
4 5

这个二叉树的最大深度为3.

解题

递归方式求树的深度,记住考研时候考过这一题

Java程序:

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
// write your code here
if(root==null)
return 0;
int res = 0;
res = depth(res,root);
return res;
}
public int depth(int res,TreeNode root){
if(root==null)
return res;
if(root.left==null && root.right==null)
return res+1;
int res1=depth(res,root.left)+1;
int res2=depth(res,root.right)+1;
res = Math.max(res1,res2);
return res;
}
}

总耗时: 2586 ms

"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: The root of binary tree.
@return: An integer
"""
def maxDepth(self, root):
# write your code here
res = 0
res = self.depth(res,root)
return res def depth(self,res,root):
if root==None:
return 0
if root.left==None and root.right==None:
return res+1
res1 = self.depth(res,root.left)+1
res2 = self.depth(res,root.right)+1
res = res1 if res1>res2 else res2
return res

总耗时: 835 ms

lintcode :二叉树的最大深度的更多相关文章

  1. 【Lintcode】二叉树的最大深度 - 比较简单,用递归比较好,不递归也能做,比较麻烦

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的 ...

  2. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度

    求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  5. LeetCode(104):二叉树的最大深度

    Easy! 题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null, ...

  6. [Leetcode] Maximum depth of binary tree二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  7. 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

    [104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...

  8. LeetCode初级算法--树01:二叉树的最大深度

    LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...

  9. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

随机推荐

  1. [大牛翻译系列]Hadoop(11)MapReduce 性能调优:诊断一般性能瓶颈

    6.2.4 任务一般性能问题 这部分将介绍那些对map和reduce任务都有影响的性能问题. 技术37 作业竞争和调度器限制 即便map任务和reduce任务都进行了调优,但整个作业仍然会因为环境原因 ...

  2. PHP弱类型安全问题笔记

    一.类型转换问题    intval(); var_dump(intval('1asdfasd'));  //1 var_dump(intval('awqw12'));  //0 var_dump(i ...

  3. centos问题集锦

    一. 为什么新装的centos系统无法使用xshell,putty等工具连接? 原因:sshd服务没有启动. 解决: 1)使用命令rpm -qa | grep ssh查看是否已经安装了ssh 2)使用 ...

  4. 关于不同进制数之间转换的数学推导【Written By KillerLegend】

    关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...

  5. Linq中延迟查询和立即查询

    //立即查询 public static void NowExecute() { var results = new int[]{5,4,3,2,1,6,7,8,9,0 }; int i = 0; v ...

  6. Linq--扩展方法

    如果现在有一个这样的需求,求筛选出来的大于20MB的进程的和,常用的方法是写一个静态方法传进去一个ProcessData列表 比如: public static Int64 TotalMemory( ...

  7. openerp经典收藏 深入理解报表运行机制(转载)

    深入理解报表运行机制 原文:http://blog.sina.com.cn/s/blog_57ded94e01014ppd.html 1) OpenERP报表的基本运行机制    OpenERP报表的 ...

  8. 一个订单相关的存储过程(MySQL)

    BEGIN DECLARE currentDate VARCHAR(15) ;/*当前日期,有可能包含时分秒 */ DECLARE maxNo INT DEFAULT 0 ; /* 离现在最近的满足条 ...

  9. nginx搭建流媒体服务器的方法详解

      一.FLV视频发布方式简介 FLV视频有两总发布方式 1.  HTTP方式 这种方式要下载FLV视频文件到本地播放,一旦FLV视频文件下载完成,就不会消耗服务器的资源和带宽,但是拖动功能没有RTM ...

  10. 横轴墨卡托 (Transverse Mercator) 投影

    横轴墨卡托 (Transverse Mercator) 投影 描述 此投影又称为高斯-克吕格投影,它与墨卡托投影相似,不同之处在于圆柱是沿经线而非赤道纵向排列.通过这种方法生成的等角投影不会保持真实的 ...