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. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public int minDepth(TreeNode root) {
  12. if(root == null) return 0;
  13.  
  14. int leftmin = minDepth(root.left);
  15. int rightmin = minDepth(root.right);
  16.  
  17. if(leftmin==0) return rightmin + 1;
  18. if(rightmin==0) return leftmin + 1;
  19. return leftmin>rightmin?rightmin+1:leftmin+1;
  20. }
  21. }

LeetCode OJ 111. Minimum Depth of Binary Tree的更多相关文章

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

  2. 【LeetCode OJ】Minimum Depth of Binary Tree

    Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...

  3. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

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

  6. LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)

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

  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 My Solution: Minimum Depth of Binary Tree

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

随机推荐

  1. VS2013中使用QT插件后每次重新编译问题

    环境 系统:win7 64位旗舰版 软件:VS2013.QT5.5.1-32位.Qt5 Visual Studio Add-in1.2.4 概述 使用QT Visual Studio插件打开pro项目 ...

  2. span设置宽和高当没有内容的时候也可撑开

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. javascript DOM对象(1)

    0.文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法. DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 将HTML代码分解为D ...

  4. Ubuntu下安装composer及配置

    1.下载最新composer wget -c https://getcomposer.org/composer.phar 2.可执行权限 chmod u+x composer.phar 3.放置到安装 ...

  5. Jmeter:相应断言介绍

    Jmeter进行性能测试时,作为对上一个请求返回信息的校验,基本上断言是不可少的,今天主要介绍一下Jmeter的相应断言校验. 相应断言:即对服务器相应信息的校验判断,发送http请求后,对服务器返回 ...

  6. 不容忽略的——CSS规范

    一.CSS分类方法: 公共型样式 特殊型样式 皮肤型样式 并以此顺序引用 <link href="assets/css/global.css" rel="style ...

  7. 淘淘商城_day07_课堂笔记

    今日大纲 讲解订单系统 基于订单系统完成下单功能的开发 使用Solr完成商品的搜索功能 订单系统 说明:订单系统只是做讲解,不做开发. 导入taotao-order 表结构 订单表: 订单商品表: 疑 ...

  8. 浏览器 CSS 兼容写法的测试总结

    做前端最讨厌的就是 IE6,7,8,虽然被淘汰的浏览器,但是在中国用户仍然很多,不可能像国外网站一样直接就不管它了,这样会流失很多流量啊. 现在有了IE9,IE10还好些,几乎和 Chrome,Fir ...

  9. C#Stopwatch的使用,性能测试

    一,先开启开始或继续测量某个时间间隔的运行时间,然后停止,最后重置时间,输出. using System; using System.Collections.Generic; using System ...

  10. 2016年团体程序设计天梯赛-决赛 L1-1. 正整数A+B(15)

    本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空格分开.问题是A和B不一定是满 ...