题目:

二叉树的最小深度

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

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

样例

给出一棵如下的二叉树:

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. Android Material Design:ViewPager与android.support.design.widget.TabLayout双向交互联动切换

    通常,android.support.design.widget.TabLayout与Android的ViewPager联合使用,实现与ViewPager的切换与联动.(1)比如,当用户手指触摸选择T ...

  2. 扩展 delphi 线程 使之传递参数.

    新delphi的线程TThread有了CreateAnonymousThread方法,如果再为它加一个可传递的参数不就更好了吗?代码如下: TAnonymousThreadX<T> = c ...

  3. python的各种推导式(列表推导式、字典推导式、集合推导式)

    推导式comprehensions(又称解析式),是Python的一种独有特性.推导式是可以从一个数据序列构建另一个新的数据序列的结构体. 共有三种推导,在Python2和3中都有支持: 列表(lis ...

  4. Spark 大数据平台 Introduction part 2 coding

    Basic Functions sc.parallelize(List(1,2,3,4,5,6)).map(_ * 2).filter(_ > 5).collect() *** res: Arr ...

  5. Basic Vlan Configure

    Basic Vlan CLI Configure Switch>en Switch#conf t Enter configuration commands, one per line.  End ...

  6. IIS支持PHP

    1. 解压php-5.2.6.zip到D:\php5,找到php.ini-dist改名为php.ini并将它放到C:\WINDOWS目录下. 2. 将D:\ php5目录下的libmcrypt.dll ...

  7. NOSQL的应用,Redis/Mongo

    NOSQL的应用,Redis/Mongo 1.心路历程 上年11月份来公司了,和另外一个同事一起,做了公司一个移动项目的微信公众号,然后为了推广微信公众号,策划那边需要我们做一些活动,包括抽奖,投票. ...

  8. cxf简单实例

    CXF是一个基于 Servlet 技术的 SOA 应用开发框架,简单来说,就是WebService的轻量级实现. 1.下载开发包:http://cxf.apache.org/download.html ...

  9. Interview-Largest independent set in binary tree.

    BT(binary tree), want to find the LIS(largest independent set) of the BT. LIS: if the current node i ...

  10. c++ uuid生成法则

    http://www.jb51.net/LINUXjishu/39614.html CentOS #include <uuid/uuid.h> 找不到文件解决方法: sudo yum in ...