题目:

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. zookeeper学习记录

    ZooKeeper:是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.他主要用来解决分布式应用中的数据管理的一致性问题 ...

  2. Linux使用Shell脚本实现ftp的自动上传下载

    1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地的/home/databackup#####!/bin/bashftp -n<<!open 1 ...

  3. mariadb主从复制架构学习笔记

    复制功用: 数据分布 负载均衡:读操作,适用于读密集型的应用 备份 高可用和故障切换 MySQL升级测试 在从服务器上有两个线程: I/O线程:从master请求二进制日志信息,并保存至中继日志 SQ ...

  4. android 的通知管理

    1在context里定义通知管理器(NotificationManager) NotificationManager notificationManager = (NotificationManage ...

  5. PHP 把GBK编码转换为UTF8

    //把GBK编码转换为UTF8 $name="勿以善小而不为"; $name=iconv("GBK", "UTF-8", $name);

  6. Hive启动时的棘手问题的处理

    Hive是存在于Hadoop集群之上的数据仓库,作为大数据处理时的主要工具,对于大数据开发人员的重要性不言而喻.当然要使用Hive仓库的前提就是对于hive的安装,hive的安装是很简单的过程,主要关 ...

  7. 简答的理解C语言中的各种类型函数

    1.变参函数 变长参数的函数即参数个数可变.参数类型不定 的函数.最常见的例子是printf函数.scanf函数和高级语言的Format函数.在C/C++中,为了通知编译器函数的参数个数和类型可变(即 ...

  8. Linux中JDK环境变量的配置

    在JDK安装好以后,需要进行环境变量的配置 配置目录   /etc/profile 在这个文件的末尾追加 JAVA_HOME=/home/j2sdk1.4.2_11PATH=$PATH:/home/j ...

  9. ORA-08189

    OS: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: Oracle Database 11g E ...

  10. jquery 简单弹出层

    预定义html代码:没有 所有代码通过js生成和移除. 预定义css .z-popup-overlay{ width:100%; min-height: 100%; height:800px; pos ...