题目

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

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

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

示例:

给定二叉树 [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. [转]创建一个JavaScript弹出DIV窗口层的效果

    本文转自:http://www.soso.io/article/23698.html <!doctype html> <html lang="en"> &l ...

  2. Linux下的NFS快速配置教程与安全策略

    [51CTO专稿]在Linux下实现文件共享有多种方式,NFS就是其中之一.网络文件系统(NFS)协议是由Sun MicroSystem在20世纪80年代为了提供对共享文件的远程访问而设计和实现的.该 ...

  3. spring的IOC和AOP详细讲解

    1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是有这个IOC容器进行产生,同时, ...

  4. mapreduce总结

    一.mapreduce简介 MapReduce是一种分布式计算模型,是hadoop的核心组件之一,是Google提出的,主要用于搜索领域,解决海量数据的计算问题. MR有两个阶段组成:Map和Redu ...

  5. Win10 手工安装.netframework3.5

    1.装载win10/win8...等镜像文件 2.cmd命令管理员身份运行 3.输入命令:C:\Windows\system32>dism.exe /online /enable-feature ...

  6. Android Studio 创建虚拟机失败 Failed to load 解决办法

    Name: Nexus_S_API_21 CPU/ABI: ARM (armeabi) Path: H:\Users\Pavkoo\.android\avd\Nexus_S_API_21.avd Er ...

  7. 夜色的 cocos2d-x 开发笔记 03

    本章添加敌人,首先我们在.h文件里添加新的方法 之后进入.cpp文件,写出方法内容 当然还要调用一次,我把这个方法添加在了这里,也就是和发子弹是同步的,当然想要多久调用一次大家可以自己调整 运行一下 ...

  8. 通过adb获取应用的Activity堆栈信息

    获取所用应用 adb shell dumpsys activity 获取自己的应用 adb shell dumpsys activity | grep 应用的package 获取处于栈顶的activi ...

  9. JavaMail 的简单使用

    概述 邮件功能模块在大多数网站中,都是必不可少的功能模块.无论是用户注册还是重置密码,邮件都是比较常用的一个方式.本文主要介绍 JavaMail 的简单使用,方便大家快速开发,供大家参考.完整的 de ...

  10. Js Date类型

    一:格式化方法 var box=new Date(); //标准时间,如果没传参数,得到的时间为当前时间 //alert(Date.parse('4/12/2007')); //11763072000 ...