leetcode-Given a binary tree, find its minimum depth
第一题
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.
返回一个二叉树的最短深度,即从根节点到叶子节点的所有路径中中,包含最少节点的那条路径上的节点个数
public class Solution {
public int run(TreeNode root) {
if(root == null)
return 0;
int i = run(root.left);
int j = run(root.right);
return (i == 0 || j == 0) ? i+j+1 : Math.min(i, j)+1;
}
}
这段代码是网上大神的,使用了递归的思想。
当遇到空节点(即叶子节点的子节点)时,返回0,表示叶子节点的子树的最短深度为0(叶子节点没有子树)。对于非叶子节点,利用run函数得到其左右子树的最短深度,将其中比较短的那条子树的最短深度+1(要加上本节点)后返回,特别的,如果其左右子树中的有一条为空,则返回非空子树的最短深度+1,只是因为当存在空子树时,过此点的最小深度路径之经过非空的那条子树。当然,如果左右子树都为空,说明当前节点为叶子节,返回值为1。
leetcode-Given a binary tree, find its minimum depth的更多相关文章
- [LeetCode]题解(python):111 Minimum Depth of Binary Tree
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- 使用idea创建web项目
一直使用的是eclipse,有一个项目开发用的是idea,我也尝试着熟悉一下idea,先来创建一个web项目吧 1.idea下载安装使用 官方下载地址:https://www.jetbrains.co ...
- 不同浏览器Firefox、IE6、IE7、IE8、IE9定义不同CSS样式
有时候我们在制作网页的时候,会遇到不同浏览器,对填充和边距显示的不同效果.导致心情纳闷现在提供解决这个困扰的方法! 对FF.Opear等支持Web标准的浏览器与比较顽固的IE浏览器进行针对性的CSS ...
- Gitlab团队协作流程
一:加入群组 项目管理员添加用户到群组,赋予权限(owner.developer...). 二:拉取项目,建立分支 通过git clone拉取项目到本地,通过终端打开项目目录,创建自己的分支,并推送到 ...
- P Invoke struct结构
一.获取Struct CHCNetSDK.NET_DVR_PTZPOS pos = new CameraTest.CHCNetSDK.NET_DVR_PTZPOS(); int size = Mars ...
- SQL DCL 数据控制语句
前言 DCL(Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许可和访问级别的语句.这些语句定义了数据库.表.字段.用户的访问权限和安全级别.主要的语句关键字包 ...
- spring 中单例 bean 初始化之后和销毁之前执行指定动作 postconstruct 和 preDestroy
1 生命周期方法, 在指定bean 创建完成后执行初始化动作或销毁之前做一些善后动作.有 3 种方法 1)实现接口 InitializingBean 然后实现 afterPropertiesSet 方 ...
- Unity3D修改LWRP,HDRP的几项小问题及解决
最近在看Book of the Dead的demo,其中对HDPR进行修改以构建自己的SRP,于是自己尝试了下.. 一般直接去Github下载对应unity版本的SRP工程: https://gith ...
- Linux应该知道的技巧
https://coolshell.cn/articles/8883.html https://www.quora.com/Linux/What-are-some-time-saving-tips-t ...
- Asp.Net Nuget常用命令
1.安装 Install-Package EntityFramework //ef Install-Package EntityFramework.zh-Hans //ef中文
- Vuex详解
一.什么是Vuex 官网解释如下: Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex ...