LeetCode111.二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [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.二叉树的最小深度的更多相关文章
- [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 ...
- [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 ...
- lintcode : 二叉树的最小深度
题目: 二叉树的最小深度 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 ...
- LeetCode 二叉树的最小深度
计算二叉树的最小深度.最小深度定义为从root到叶子节点的最小路径. public class Solution { public int run(TreeNode root) { if(root = ...
- lintcode 155 二叉树的最小深度
二叉树的最小深度 描述 笔记 数据 评测 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Ai ...
- 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- Leecode刷题之旅-C语言/python-111二叉树的最小深度
/* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...
- 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 ...
- leetcode 111. 二叉树的最小深度
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...
随机推荐
- Js高设笔记1-2章 defer and async
1,js是由ECMAscript ,dom ,bom组成的专为网页交互而设计的脚本语言.js是脚本语言之一. 2,MIME,是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的 ...
- PHP之伪类型与变量
本文档中使用的伪类型与变量 伪类型(pseudo -types)是PHP文档里用于指示参数可以使用的类型和值, 请注意,它们不是PHP语言里原生类型, 所以不能把伪类型用于自定义函数里面的类型约束(t ...
- Aspects源码解析(转载)
文章来源:https://www.jianshu.com/p/2c93446d86bd
- [knownledge][latex] LaTex入门
序言 最近需要写一份文档, 时间也不是特别紧. 之前一直用markdown写文档. 始终想学一下LaTex, 毕竟是学术论文界的工具. 在提及LaTex的内容之前. 事必是一定要首先提及高德纳的. 他 ...
- ANT入门&用ANT编译java项目
第一次接触ant是15年在无锡某软件公司实习时,当时的项目是由多个模块组成,开发分成模块开发的几个小组.为了提高开发效率,采用这种编译项目的方法. 最近接触到flex项目,采用eclipse自动编译的 ...
- 压缩维度oj P1173+P1174+P1164
今天在洛谷上刷dp,忽然冒出一道求最大字段和的问题,然后忘了瞬间忘了这是dp,几分钟一个贪心出来了成功ac,忽然想起自己在作dp,于是乖乖刷dp. 这个可能很多人都会但是今天有4种解法哦,本人只尝试了 ...
- kibana设置mapping
demo: PUT linewell_assets_mgt_fz_es/lw_devices/_mapping { "properties": { "update_tim ...
- 安装sqlserver2008中出现的问题小结
安装完sqlserver2008时报了几个错,但是好歹装上了,但是我想使用sa用户登录,给我出现了这么一个错 标题: 连接到服务器------------------------------ 无法连接 ...
- 图解HTTP笔记
http 0.9 1990年 http 1.0 1996年 Tcp/Ip分层模型 应用层: 如Ftp Http DNS. 传输层: 如 TCP UDP .将报文分成报文段.“传输”指的是端对端( ...
- 【托业】【全真题库】TEST01-03-阅读题
[托业][全真题库]TEST01-03-阅读题