题目

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

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

返回它的最小深度  2.


考点

dfs.

递归


思路


代码

1.递归

/**
* 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) {
//root==nullptr,0
if(!root)
return 0;
//left==nullptr->f(right)+1
if(!root->left) return 1+minDepth(root->right);
//right==nullptr->f(left)+1
if(!root->right) return 1+minDepth(root->left);
//none of above==nullptr->min(f(left),f(right))+1
return 1+min(minDepth(root->left),minDepth(root->right));
}
};

问题

LeetCode111. Minimum Depth of Binary Tree的更多相关文章

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

  2. 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 ...

  3. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  4. 【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 ...

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

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

  6. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

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

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

  8. 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 ...

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

随机推荐

  1. 配置matcaffe 遇到的两个坑

    1. 问题 (1) Invalid MEX-file '/root/caffe/matlab/+caffe/private/caffe_.mexa64': /matlab/r2016a/bin/gln ...

  2. [转]jQuery.getJSON的缓存问题的解决办法

    本文转自:http://mfan.iteye.com/blog/974132 今天做测试工作,发现了一个令我费解的问题,jquery的getJson方法在firefox上运行可以得到返回的结果,但是在 ...

  3. OpenStack Weekly Rank 2015.08.24

    Module Reviews Drafted Blueprints Completed Blueprints Filed Bugs Resolved Bugs Cinder 5 1 1 6 13 Sw ...

  4. 换晶振导致stm32串口数据飞码的解决办法(补充)

    今天(2014.4.21)把stm32f107的程序下载到stm32f103的板子上,发现串口收不到数据,突然想起晶振频率没有修改,#define HSE_VALUE    ((uint32_t)13 ...

  5. 【ubuntu】出现device not managed连接不上网络

    ubuntu安装好后显示“device not managed” 1. 编辑/etc/NetworkManager/NetworkManager.conf: sudo gedit /etc/Netwo ...

  6. Tomcat启动过程[更详细]

    原文地址:http://blog.csdn.net/jiaomingliang/article/details/47427083

  7. JavaSE之Java基础(1)

    1.为什么重写equals还要重写hashcode 首先equals与hashcode间的关系是这样的: 1.如果两个对象相同(即用equals比较返回true),那么它们的hashCode值一定要相 ...

  8. linux创建用户与删除用户及问题解决(ubuntu)

    创建的用户不正确,一直在删除创建,发现了挺多问题也学到了一些东西如下是我的总结. (root用户设置: 由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在终端 ...

  9. php cur错误:SSL错误 unable to get local issuer certificatebool

    采集https链接时出现的问题 办法:跳过SSL证书检查 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLO ...

  10. 将Android工程打成jar包之后对资源的调用。

    Android工程不能完整的打包成jar包.这个主要是R文件导致的,但是我们可以将除了资源文件中的所有东西都打到jar包中.Activity.View等等类. 提供jar包的同时,还需要提供资源文件. ...