【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 ...
随机推荐
- C# 隐藏标题栏 调整大小 并且移动窗口
隐藏标题栏(窗口属性): 1.设置在该窗体的标题栏中是否显示控件框: this.ControlBox = false; 2.设置在该窗体的标题为空: this.Text = string.Empty; ...
- [译]Dynamics AX 2012 R2 BI系列-Cube概览
https://msdn.microsoft.com/EN-US/library/dd252604.aspx Cube是一个多维度的结构,它是BI应用开发的基础.本文描述了cube的组成部分, ...
- FTP连接时出现“227 Entering Passive Mode” 的解决方法
今天从公网的服务器连接本地内网的FTP server copy文件时,系统老是提示227 Entering Passive Mode (xxx,xxx,,xxx,xxx,x),很是奇怪,于是上网找资料 ...
- 小结一下前段时间做的rpgdemo
虽然说已经是彻底放弃继续做那个demo了(代码结构混乱,想增加新功能非常的不方便),不过还是花了一点心血在里面的,毕竟这是我开始学习unity游戏制作的初衷,不过果然是学的越多越发现自己的不足... ...
- 随便玩玩系列之一:SPOJ-RNG+51nod 算法马拉松17F+51nod 1034 骨牌覆盖v3
先说说前面的SPOJ-RNG吧,题意就是给n个数,x1,x2,...,xn 每次可以生成[-x1,x1]范围的浮点数,把n次这种操作生成的数之和加起来,为s,求s在[A,B]内的概率 连续形的概率 假 ...
- What's the difference between <b> and <strong>, <i> and <em> in HTML/XHTML? When should you use each?
ref:http://stackoverflow.com/questions/271743/whats-the-difference-between-b-and-strong-i-and-em The ...
- JS控制的几种页面跳转方式和传值
第一种:<script language="javascript" type="text/javascript">window.location.h ...
- qt creator 使用Ui文件的问题
一.显式地调用uic.exe 如何将UI文件生成头文件 1,将设计的UI文件拷贝到uic.exe的目录下Qt\Qt5.3.2\5.3\mingw482_32\bin. 2, 打开windows的CMD ...
- Windows 服务快捷启动命令
gpedit.msc-----组策略sndrec32-----录音机nslookup----- ip地址侦测器explorer------ 打开资源管理器logoff-------注销命令tsshut ...
- 深入理解IOC模式及Unity框架
研究了下,有几篇博客确实已经说得很清楚了 1.IoC模式:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html 这篇博客是通过一个 ...