一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:求二叉树的最小高度。最小高度是指,从根节点到叶子节点的最短路径上的节点数。

解题思路:采用递归的思想,以二叉树上的每一个节点作为根节点,那么它的最小高度等于左子树和右子树的最小高度加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) {
        if(root==NULL) return 0;//如果树为空,返回0
        return dfsTree(root);
    }
    int dfsTree(TreeNode* root)
    {
        if(root==NULL) return INT_MAX;//这里用到一个技巧,节点为空返回最大值,后面不用判断那个树为空
        int left = dfsTree(root->left);//求左子树的最小高度
        int right = dfsTree(root->right);//求右子树的最小高度
        if(left==INT_MAX&&right==INT_MAX) return 1;//如果左右子树都为空,则返回1
        return min(left,right)+1;//否则就返回左右子树中最小高度较小的那一个
    }
};

【一天一道LeetCode】#111. Minimum Depth of Binary Tree的更多相关文章

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

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

  2. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  4. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  6. leetcode 111 Minimum Depth of Binary Tree ----- java

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. Java [Leetcode 111]Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  8. (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. Java for LeetCode 111 Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  10. leetcode 111 Minimum Depth of Binary Tree(DFS)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. Java8-理解Colloctor

    上一节学习了Java8中比较常用的内置collector的用法.接下来就来理解下collector的组成. Collector定义 Collector接口包含了一系列方法,为实现具体的归约操作(即收集 ...

  2. sssp-springmvc+spring+spring-data-jpa增删改查

    环境:IDE:eclipse.jdk1.7.mysql5.7.maven 项目结构图 上面目录结构你可以自己创建 搭建框架 首先加入maven依赖包以及相关插件 <dependencies> ...

  3. diango-团队介绍

    1.使用django-admin startproject show创建项目,并使用python manage.py startapp team_show创建应用 2.进行相关的配置 3.代码的实现

  4. mysql常见的优化需要注意的点

    1.explain分析explian引用索引基数show indexes from table_name;主键索引具有最好的基数 测试时 不走缓存SELECT SQL_NO_CACHE id from ...

  5. 如何去掉修改Joomla、joomlart及其模版版权、标志、图标的方法

    Joomla是遵循GNU通用公共授权(GPL)的自由软件,我们虽然不推荐将Joomla的所有版权删除,但有些必要的信息还是需要修改的,下面以JoomlArt.com 的JA_teline_iii_v2 ...

  6. 一些重要的计算机网络协议(IP、TCP、UDP、HTTP)

    一.计算机网络的发展历程 1.计算机网络发展 与其说计算机改变了世界,倒不如说是计算机网络改变了世界.彼时彼刻,你我都因网络而有了交集,岂非一种缘分? 计算机与网络发展大致经历如下过程:

  7. 中间件——canal小记

    接到个小需求,将mysql的部分数据增量同步到es,但是不仅仅是使用canal而已,整体的流程是mysql>>canal>>flume>>kafka>> ...

  8. 浅析JS异步执行机制

    前言 JS异步执行机制具有非常重要的地位,尤其体现在回调函数和事件等方面.本文将针对JS异步执行机制进行一个简单的分析. 从一份代码讲起 下面是两个经典的JS定时执行函数,这两个函数的区别相信对JS有 ...

  9. Python笔记十一(迭代器)

    这里我们要学会Iterable和Iterator. 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的generator f ...

  10. Java9相关资料(JShell简易教程等)

    资源 Java9官网下载地址 Java9官方教程 JShell(Java Shell) 参考资料: JShell User Guide Java9先睹为快:JShell动手实践 以下大部分内容均来自该 ...