【JAVA】【leetcode】【查找二叉树最小深度】
题目: minimum-depth-of-binary-tree
要求: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 TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public int run(TreeNode root) {
if(root!=null){
int left = Integer.MAX_VALUE;
int right = Integer.MAX_VALUE;
if(root.left!=null){
left = run(root.left);
}
if(root.right!=null){
right = run(root.right);
}
if(left<right){
return left+1;
}
else if(left>right){
return right+1;
}
else if(left==right&&left!=Integer.MAX_VALUE){
return left+1;
}
else
return 1;
}
return 0;
}
}
当然,还有一个较简洁的递归方法,最核心的思想就是根节点到达最近的叶子节点的路径长度:
1、当根为空时,输出0
2、当左子树为空时,输出右子树深度+1
3、当右子树为空时,输出左子树深度+1
4、以上条件都不满足时,输出min(左子树深度,右子树深度)+1
public class Solution {
public int run(TreeNode root) {
if(root==null)
return 0;
if(root.left==null)
return run(root.right)+1;
if(root.right==null)
return run(root.left)+1;
return Math.min(run(root.left),run(root.right))+1;
}
}
【JAVA】【leetcode】【查找二叉树最小深度】的更多相关文章
- Java实现查找二叉树&C++的做法
写了个Java的查找二叉树,用递归做的,不用递归的还没弄出来.先贴一下.回头再研究. BTreeTest.java: public class BTreeTest{ class Node{ Node ...
- [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 ...
- LinCode 刷题 之二叉树最小深度
http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/ 题目描述信息 /** * Definition of Tree ...
- leetcode-111. 二叉树最小深度 · Tree + 递归
题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...
- LeetCode111_求二叉树最小深度(二叉树问题)
题目: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the s ...
- 剑指offer-第六章面试中的各项能力(二叉树的深度)
题目:1:输入一个二叉树,求二叉树的深度.从根节点开始最长的路径. 思路:我们可以考虑用递归,求最长的路径实际上就是求根节点的左右子树中较长的一个然后再加上1. 题目2:输入一颗二叉树的根节点,判断该 ...
- Java实现 LeetCode 111 二叉树的最小深度
111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...
- 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 ...
- [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 ...
随机推荐
- [转] 经典SQL练习题
原题目来自qaz13177_58_CSDN博客 http://blog.csdn.net/qaz13177_58_/article/details/5575711/#sql 只是更新个人答案供参考 表 ...
- get([index])
get([index]) 概述 取得其中一个匹配的元素. num表示取得第几个匹配的元素.从0开始,返回的是DOM对象,类似的有eq(index),不过eq(index)返回的是jQuery对象. 这 ...
- Make cnblogs mobile Compatible
Introduction 博客园的许多页面都是只支持PC的,没有手机端的,也没有手机端的app.每次查看都有放大了才能看清楚字体,手指和游泳似的,左右开弓,很不方便.还有上次我修改了theme之后,手 ...
- error: could not read CFBundleIdentifier from Info.plist (null)解决方法之一
出现这种错误的原因可能很多,以下是我遇到的一种情况: 项目移植到新的环境 编译报错: error: could not read CFBundleIdentifier from Info.plist ...
- [转]SqlSever2005 一千万条以上记录分页数据库优化经验总结【索引优化 + 代码优化】一周搞定
对普通开发人员来说经常能接触到上千万条数据优化的机会也不是很多,这里还是要感 谢公司提供了这样的一个环境,而且公司让我来做优化工作.当数据库中的记录不超过10万条时,很难分辨出开发人员的水平有多高,当 ...
- Git使用指南(3)—— 使用Git命令
暂存区替换掉工作区 git init git init newrepo 克隆仓库 git clone git clone <repo> git clone <repo> < ...
- JAVA模拟HTTP post请求上传文件
在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较 ...
- 将自己库添加Cocoapods支持
给库添加Cocoapods支持, 使这个工具使用起来更加方便, 更好的使用Cocoapods, 助力iOS程序开发, 下面进入正题, 想要实现这个过程, 绝对不虚此读. 首先写好一个要添加Cocoap ...
- EX14 彩票中奖 (lottery.pas/c/cpp)
[题目描述]小明想试试运气去购买彩票,所以他开始研究彩票大乐透的玩法:超级大乐透是指由购买者从01—35共35个号码中选取5个号码为前区号码,并从01—12共12个号码中选取2个号码为后区号码组合为一 ...
- 迈出第一步:自适应高度的ImageView(AutoHeightImageView)
这个博客注册很久了,可是一直都没有勇气来写一点东西.今天解决了一个让我纠结很久的问题,于是,我决定开始我的博客生涯,希望我能坚持下去. 不知道是不是只有我遇到了这个问题,在ListView中显示图片, ...