题目:

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.

代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) return ;
if ( !root->left && !root->right) return ;
if ( root->left && root->right ) return std::min( Solution::minDepth(root->left)+, Solution::minDepth(root->right)+);
if ( root->left ) return Solution::minDepth(root->left)+;
if (root->right ) return Solution::minDepth(root->right)+;
}
};

tips:

深搜思路(递归实现)。这里需要控制向下进行的条件。

1. 如果root是NULL,返回0

2. 如果root不是NULL,且left和right都是NULL,则到达叶子节点返回1(代表算上叶子节点的那一层)

3. 如果root->left和root->right都不为NULL,则继续往两边深搜

4. 如果root不是NULL,但root->left或root->right哪一方为NULL,则为NULL的一端不会再有叶子节点出现,不能再往下走了。

完毕。

============================================

第二次过这道题,上来没有把终止条件想完全。终止条件应该是达到叶子简单root->left root->right都为空。

修改了一次后AC了。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root)
{
if ( !root ) return ;
int min_depth = INT_MAX;
Solution::depth(root, min_depth, );
return min_depth;
}
static void depth(TreeNode* root, int& min_depth, int dep)
{
if ( !root->left && !root->right ) min_depth = min(min_depth,dep);
if ( root->left ) Solution::depth(root->left, min_depth, dep+);
if ( root->right ) Solution::depth(root->right, min_depth, dep+);
}
};

【Minimum Depth of Binary Tree】cpp的更多相关文章

  1. 【二叉树的递归】01二叉树的最小深度【Minimum Depth of Binary Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...

  2. 【Maximum Depth of Binary Tree 】cpp

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

  3. 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...

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

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

  5. 【LeetCode练习题】Minimum 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 (2 solutions)

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

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

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

  8. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

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

  9. [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 ...

随机推荐

  1. linux环境配置

    一.JDK安装 1.通过xftp工具把jdk-8u60-linux-x64.gz上传到linux 2.解压JDK命令tar -xzf jdk-8u60-linux-x64.gz 3.linux配置环境 ...

  2. CentOS学习笔记--基本命令--文件与目录管理

    Linux基本命令--文件与目录管理 本节节选自鸟哥的 Linux 私房菜 -- 基础学习篇目录  第七章.Linux 文件与目录管理  ls(文件与目录的检视) ls命令就是list的缩写,ls可以 ...

  3. SVN命令收集

    1.检出 svn co  http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码 svn  co  svn://路径(目录或文件的全路径 ...

  4. Curses library not found. Please install appropriate package

    今天安装mysql-5.5.3-m3的时候,报下面的错误: -- Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_ ...

  5. [leetcode]_Sum Root to Leaf Numbers

    题目:计算一棵二叉树所有路径组成的数的总和. 思考:也是DFS的基础应用.虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程. 代码: private int sum = 0; ...

  6. Centos6.5环境下安装SVN 整合Apache+SSL

    弄了两天,终于在服务器上初步搭建起来了SVN(版本1.8). 服务器系统:Centos6.5 64位,搭建过程中全部采用源码编译安装(configure/make/make install),推荐大家 ...

  7. js 将json字符串转换为json兑现

    在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键.例如:JSON字符串:var str1 = '{ &quo ...

  8. 表格控件表头栏目(Column)与数据表头步

    不用手工增加栏目的列,也就是Column,由数据库的查询结果自动创建. 用的是Delphi2010,安装了Dev,用CxGrid显示数据库查询结果.用什么控件没有关键,道理相同的.

  9. .NET String.Format 方法 线程安全问题

    碰到这个问题 是在和淘宝做信息交互的时候, 接收别人N年前的代码. 代码逻辑很简单,就是取得信息 数据库查询  响应请求返回结果. 最近淘宝的人反映说 N多账户使用的是一个单号.理论上来说 是应该每次 ...

  10. UIViewAnimationOptions swift 2

    UIView.animateWithDuration(0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, ...