Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

和上一题对应,求二叉树的最小深度。

解题思路:

参考上一题Maximun Depth of Binary Tree中最后那个极短的解法。

另外需要判断一下递归返回0的时候的结果不可取,因为不是叶节点。

代码如下:

class Solution {
public:
int minDepth(TreeNode *root) {
if(!root)
return ;
int l = minDepth(root->left);
int r = minDepth(root->right);
if(l * r != )
return min(l,r)+;
else if(l == )
return r+;
else
return l+;
}
};

【LeetCode练习题】Minimum Depth of Binary Tree的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

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

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  3. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

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

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

  5. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  6. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  7. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

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

  8. [leetcode] 5. Minimum Depth of Binary Tree

    二叉树基本功练习题,题目如下: Given a binary tree, find its minimum depth. The minimum depth is the number of node ...

  9. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  10. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

随机推荐

  1. JAVA大整数傻瓜入门

    http://blog.csdn.net/skiffloveblue/article/details/7032290..先记着

  2. 04747_Java语言程序设计(一)_第6章_图形界面设计(二)

    例6.1声明一个面板子类,面板子类对象有3个选择框. class Panel1 extends JPanel { JCheckBox box1, box2, box3; Panel1() { box1 ...

  3. [每日一题] OCP1z0-047 :2013-08-17 EXTERNAL TABLE――加载数据 ............................56

    正确答案:C 一.对答案解释: A.       TYPE:有两个选可供选择: 1.        ORACLE_LOADER:传统方式,与SQLLDR一样,参数从多,应用较多. 2.         ...

  4. 计划任务中使用NT AUTHORITY\SYSTEM用户和普通管理员用户有什么差别

    原文地址:http://www.ynufe.edu.cn/metc/Article/ShowArticle.asp?ArticleID=805 系统管理员会碰到这种问题,为什么在更改系统登录用户pas ...

  5. UIButton-初识IOS

    今天,我学到了所有app经常用到的UIButton控件,废话不多说,这些都是我学习的时候总结的一些,希望可以帮到以后的初学者,IOS初学不应该直接拖拽,感觉不易于理解,所以我总结的基本上全是纯代码编辑 ...

  6. C#枚举器接口IEnumerator的实现

    原文(http://blog.csdn.net/phpxin123/article/details/7897226) 在C#中,如果一个类要使用foreach结构来实现迭代,就必须实现IEnumera ...

  7. 更新Android SDK 出错 Failed to rename directory \temp\ToolPackage.old01

    打算更新Android SDK 版本到 Android SDK Tools 20,打开SDK Manager.exe 开始更新,结果安装时弹出错误提示:Failed to rename directo ...

  8. Cisco cmd 命令

    1.enable 开启全局配置模式:disable 禁用配置模式 2.config进入配置模式 3.line 设置进入用户模式密码:分为 line aux 0;line console 0;line ...

  9. Ngui _CD技能特效

    using UnityEngine;using System.Collections; public class Skill : MonoBehaviour { public float coldTi ...

  10. vim 折叠技巧

    转自:http://www.2cto.com/os/201203/122133.html 主要命令: zf-创建折叠 zf20G--创建折叠,从当前行折叠到第20行 zfgg--创建折叠,从当前位置折 ...