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

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

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

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

返回它的最小深度  2.

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if (root == null)return 0;
if (root.left == null || root.right == null) {
return Math.max(minDepth(root.left),minDepth(root.right))+1;
}
return Math.min(minDepth(root.left),minDepth(root.right))+1;
}
}

LeetCode111.二叉树的最小深度的更多相关文章

  1. [Swift]LeetCode111. 二叉树的最小深度 | Minimum Depth of Binary Tree

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

  2. [LeetCode] 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. lintcode : 二叉树的最小深度

    题目: 二叉树的最小深度 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 样例 给出一棵如下的二叉树: 1 /     \ 2       3 /    \ 4    ...

  4. LeetCode 二叉树的最小深度

    计算二叉树的最小深度.最小深度定义为从root到叶子节点的最小路径. public class Solution { public int run(TreeNode root) { if(root = ...

  5. lintcode 155 二叉树的最小深度

    二叉树的最小深度   描述 笔记 数据 评测 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Ai ...

  6. 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

    求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  7. Leecode刷题之旅-C语言/python-111二叉树的最小深度

    /* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...

  8. 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 ...

  9. leetcode 111. 二叉树的最小深度

    题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...

随机推荐

  1. Vue的自动化测试

    前言 为什么我们需要测试? 让产品可以快速迭代,同时还能保持高质量 -- 阮一峰 持续集成是什么? 对于一些相对稳定的系统级别页面,自动化测试在提高测试的效率的方面起到非常重要的作用.前端的自动化测试 ...

  2. English class 81:How Vulnerability can make our lives better?

    1,can we come off as weak if we show imperfections? 2,The first thing I look for in you and the last ...

  3. Installing Precise (12.04.2) using netboot onto a Marvell ArmadaXP Development Board

      https://wiki.ubuntu.com/ARM/Server/Install https://wiki.ubuntu.com/ARM/Server/Install/ArmadaXP Arm ...

  4. iOS 限制输入字数

    关于限制输入字数以前也做过,网上也很多方法.但都不够完美,本方法可防止中文联想.粘贴等突破长途限制.可防止Emoji截为两半导致编码出问题. - (void)textFieldDidChange:(U ...

  5. iOS-Core-Animation-Advanced-Techniques/13-高效绘图 【没理解】

    #import "DrawingView.h" #import <QuartzCore/QuartzCore.h> @interface DrawingView () ...

  6. RHEL6.2 ORACLE11G

    今天翻文件看到这个系统的安装截图才发现自己没写,补上. 惯例还是用到的所有参数均位于文末附录 启动虚拟机进入系统安装 选择跳过硬盘检测 选择语言 选择基本存储设备 设定主机名 设定口令 选择使用所有空 ...

  7. Django + Redis实现页面缓存

    目的:把从数据库读出的数据存入的redis 中既提高了效率,又减少了对数据库的读写,提高用户体验. 例如: 1,同一页面局部缓存,局部动态 from django.views import View ...

  8. mysql分库 分表

    原文链接:http://www.jianshu.com/p/89311703b320 传统的分库分表传统的分库分表都是通过应用层逻辑实现的,对于数据库层面来说,都是普通的表和库.分库分库的原因 首先, ...

  9. websocketd

    https://www.cnblogs.com/tinywan/p/6826125.html https://www.jianshu.com/p/63afd0099565

  10. mysql 权限管理 目录

    mysql 权限管理介绍 mysql 权限管理 记录 mysql 权限管理 grant 命令 mysql 权限管理 revoke 回收权限 命令 mysql 权限管理 针对库 授权 db.* mysq ...