题目:

二叉树的最小深度

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

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

样例

给出一棵如下的二叉树:

1

/     \

2       3

/    \

4      5

这个二叉树的最小深度为 2

解题:

递归求解

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 minDepth(TreeNode root) {
// write your code here
int res = 0; return depth(res,root);
}
public int depth(int res,TreeNode root){
if(root==null)
return 0;
if(root.left==null && root.right==null)
return res+1;
if(root.left!=null && root.right==null)
return res=depth(res,root.left)+1;
if(root.left==null && root.right!=null)
return res=depth(res,root.right)+1;
int res1 = depth(res,root.left)+1;
int res2 = depth(res,root.right)+1;
res = res1>res2?res2:res1;
return res;
}
}

总耗时: 2455 ms

Python程序:

"""
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 minDepth(self, root):
# write your code here
if root==None:
return 0
if root.left==None and root.right!=None:
return self.minDepth(root.right) + 1
if root.left!=None and root.right==None:
return self.minDepth(root.left) + 1
return min(self.minDepth(root.right),self.minDepth(root.left)) + 1;

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

  1. lintcode 155 二叉树的最小深度

    二叉树的最小深度   描述 笔记 数据 评测 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Ai ...

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

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

  3. LeetCode 二叉树的最小深度

    计算二叉树的最小深度.最小深度定义为从root到叶子节点的最小路径. public class Solution { public int run(TreeNode root) { if(root = ...

  4. 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

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

  5. Leecode刷题之旅-C语言/python-111二叉树的最小深度

    /* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...

  6. LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)

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

  7. leetcode 111. 二叉树的最小深度

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

  8. Java实现 LeetCode 111 二叉树的最小深度

    111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...

  9. 【LeetCode】111. 二叉树的最小深度

    111. 二叉树的最小深度 知识点:二叉树,递归 题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 示例 输入 ...

随机推荐

  1. 解决iOS设备屏幕切换时页面造成的问题

    环境:IOS6~7 Safari 问题:iOS设备屏幕切换时可能会造成屏幕变大,出现左右间距等问题 解决方法: 头部加入<meta name = "viewport" con ...

  2. mysql 让一个存储过程定时作业的代码

    1.在mysql 中建立一个数据库 test1 语句:create database test1 2.创建表examinfo create table examinfo( id int auto_in ...

  3. php多层数组与对象的转换实例代码

    通过json_decode(json_encode($object)可以将对象一次性转换为数组,但是object中遇到非utf-8编码的非ascii字符则会出现问题,比如gbk的中文,何况json_e ...

  4. Sql Server数据库

    1.表分区 http://www.cnblogs.com/huangxincheng/p/3565755.html 2.MVP教程地址:http://www.cnblogs.com/lyhabc/p/ ...

  5. ERROR 23 (HY000) at line 29963: Out of resources when opening file

    在还原数据库时报错,报错信息如下:(库中的表比较多) ERROR 23 (HY000) at line 29963: Out of resources when opening file 解决方法: ...

  6. elipse+pydev+python开发arcgis脚本程序

    环境配置参考:http://www.cnblogs.com/halfacre/archive/2012/07/22/2603848.html 添加arcpy类库.arctoolbox.arcgis-b ...

  7. php微型mvc框架创建步骤

    创建数据库和表结构,并且添加模拟数据: 新建models.views.controllers.utilities和include五个文件夹:models:模型层     model.php:模型基类  ...

  8. ios自定义选择器ActionSheetPicker改进版

    ios自带的UIDataPicker和UIDatePicker最大的毛病就是没有带确定和取消这两个按钮,而ActionSheetPicker是以上两个选择器的开源封装.但是这个东东也有些小问题,就是没 ...

  9. foxmail创建163公司企业邮箱的时候会出现ERR Unable to log on

    foxmail创建163公司企业邮箱的时候会出现ERR Unable to log on 解决办法:把pop.qiye.163.com更改为pop.ym.163.com,瞬间创建成功....也许是网易 ...

  10. Jenkins入门-转

    reference : http://www.cnblogs.com/itech/archive/2011/11/23/2260009.html 在网上貌似没有找到Jenkins的中文的太多的文档,有 ...