一天一道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,如果没有左子树(右子树)就等于右子树(左子树)的最小高度。

具体解释见代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int minDepth(TreeNode* root) {
  13. if(root==NULL) return 0;//如果树为空,返回0
  14. return dfsTree(root);
  15. }
  16. int dfsTree(TreeNode* root)
  17. {
  18. if(root==NULL) return INT_MAX;//这里用到一个技巧,节点为空返回最大值,后面不用判断那个树为空
  19. int left = dfsTree(root->left);//求左子树的最小高度
  20. int right = dfsTree(root->right);//求右子树的最小高度
  21. if(left==INT_MAX&&right==INT_MAX) return 1;//如果左右子树都为空,则返回1
  22. return min(left,right)+1;//否则就返回左右子树中最小高度较小的那一个
  23. }
  24. };

【一天一道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. 关于Matchvs一些使用心得与建议

    我的项目是类似<贪吃蛇>玩法的一款IO游戏,就是几个玩家在游戏界面中可以吃食物,也可以相互吃,吃了食物或对方都会变大这样子.我是在用cocos creator做完前端开发的部分后,开始接入 ...

  2. jquery easyui datagrid改变某行的值

    $("#DeterminateMembers").datagrid("updateRow",{index:index,row:{fzr:"0" ...

  3. 360搜索引擎so自动收录php改写方案——适合phpcms等cms

    360搜索引擎自动收录功能,官方提供了代码,带式,十分坑爹,没有提供批量提交入口,只是提供了一段js代码,关键是 一个js去下载另外一个js,document.write到文档,然后再 重复2遍如此工 ...

  4. 06 Nexus仓储/基础设施 - DevOps之路

    06 Nexus仓储/基础设施 - DevOps之路 文章Github地址,欢迎start:https://github.com/li-keli/DevOps-WiKi Nexus仓储官网简介: Th ...

  5. C++笔记001:Microsoft Visual Studio 2010软件的安装与建立第一个cpp文件

    原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 我学习C++使用软件为Microsoft Visual Studio 2010. 首先,软件的安装包 链接:https://pan.bai ...

  6. python获取指定时间差的时间

    在分析数据的时间经常需要截取一定范围时间的数据,比如三天之内,两小时前等等时间要求的数据,因此将该部分经常需要用到的功能模块化,方便以后以后用到的时候复用.在此,也分享给大家. <span st ...

  7. MT8127:改变安卓系统权限问题

    找到对应的文件: system/extras/su/su.c 在main函数中,开头我们可以看下以下代码: uid_t current_uid = getuid(); if (current_uid ...

  8. ejabberd mod_echo 解析

    ejabberd mod_echo 解析(金庆的专栏 2016.8)按开发入门的说明,mod_echo是最简单的模块之一.https://docs.ejabberd.im/developer/当然 m ...

  9. load balancer 配置参考

    https://wiki.ewu.edu/oit/Load_balancing_using_nginx

  10. Android Studio下多渠道打包

    Android Studio下实现多渠道打包 直接上步骤 步骤 1. 清单文件添加属性(以友盟统计为例) 在application标签下添加meta-data属性 <application -- ...