题目:

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. vim与shell的切换

    方法1: vim->shell: ctrl-z (挂起vim进程,相当于图形界面中的最小化) shell->vim: fg (foreground)   方法2: vim->shel ...

  2. rpm软件包

    安装软件:rpm -i software.rpm卸载软件:rpm -e software升级形式安装:rpm -U software-new.rpmrpm支持通过http.ftp协议安装软件:rpm ...

  3. 计算2的n次方的三种方法(C语言实现)

    C代码如下: #include <stdio.h> int func1(int n) { <<n; } int func2(int n) { ) { ; } )*; } int ...

  4. Cassandra 备份 - 1 - 节点镜像恢复

    之前比较关注如何使用Cassandra,但是真正想大规模使用前提还是需要搞清楚备份机制,确保数据安全. 本文主要内容来自文档 "Cassandra2.2"的翻译.最后部分为真实操作 ...

  5. 合并果子 (codevs 1063) 题解

    [问题描述] 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和 ...

  6. 在ASP.NET MVC中以post方式传递数组参数的示例

    最近在工作中用到了在ASP.NET MVC中以post方式传递数组参数的情况,记录下来,以供参考. 一.准备参数对象 在本例中,我会传递两个数组参数:一个字符串数组,一个自定义对象数组.这个自定义对象 ...

  7. C# 笛卡尔积

    void Main() { string[] str1 = { "a", "b" }; " }; string[] str3 = { "一& ...

  8. 【转载】Android通过ksoap2调用.net(c#)的webservice

    转载自:http://www.cnblogs.com/badtree/articles/3242842.html ■下载 ksoap2-android 包 去http://code.google.co ...

  9. 第二十章 数据访问(In .net4.5) 之 使用LINQ

    1. 概述 .net3.5中新添加给C#的LINQ查询,提供了直观便捷的数据查询方式.并且支持多种数据源的查询. 本章介绍标准的LINQ操作,如何用最优的方式使用LINQ 以及 LINQ to XML ...

  10. 开始认真学计算机网络----computer network学习笔记(一)

    什么是计算机网络,就是连一堆计算机,计算机不单单指pc,还包括打印机啦,手机啦巴拉巴拉一堆 为什么要连,share data共享数据 数据? 文档,图片,视频,巴拉巴拉 网络有什么类型? LAN--- ...