一天一道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. Server-U FTP与AD完美集成方案详解

    最近咱有个任务,那就是把公司的文件服务器.FTP服务器.邮件服务器进行迁移并作相应的整合.登陆后台查看了,公司目前正在使用的方案.FTP服务器使用的是Server-u FTP,验证方式选择的windo ...

  2. JS 判断是否为IP格式

    <html> <head> <title><a href='http://js.zz5u.net'><u>JavaScript</u& ...

  3. Oracle10g以上sysaux表空间的维护和清理

    SYSAUX表空间在Oracle 10g中引入,其作为SYSTEM表空间的辅助表空间.之前,一些使用独立表空间或系统表空间的数据库组件,现在SYSAUX表空间中存在.通过分离这些组件,减轻了SYSTE ...

  4. 自定义alert窗口样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 如何在joomla上展示word,pdf,xlsx,ppt

    去年用slideshare,非常好用,只要在joomla上装上插件,就能直接把slideshare上的文件弄到网站上了,但是前几天突然发现slideshare登不进去了,而<embed>下 ...

  6. Python中的转义

    在Python交互式解释器中,输出的字符串会用引号引起来,特殊字符会用反斜杠(\)转义.如果遇到带有\的字符被当作特殊字符时,有以下两种处理方法:1.使用双反斜杠(\\)来转义2.使用原始字符串,方法 ...

  7. DotnetSpider (一) 架构的理解、应用、搭建

    第一次写博客,比较浅显,欢迎大牛们指点一二,不胜感激.   ** 温馨提示:如需转载本文,请注明内容出处.**   本文连接:http://www.cnblogs.com/grom/p/8931650 ...

  8. FJUT第四周寒假作业[JL]最后的晚餐(动态规划)

    题目来源:http://210.34.193.66:8080/vj/Contest.jsp?cid=163#P4 [JL]最后的晚餐 TimeLimit:1000MS  MemoryLimit:100 ...

  9. JavaScript for 循环

    循环可以将代码块执行指定的次数. JavaScript 循环 如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: documen ...

  10. python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作

    1.通过 pip 安装 pymysql 进入 cmd  输入  pip install pymysql   回车等待安装完成: 安装完成后出现如图相关信息,表示安装成功. 2.测试连接 import ...