题目:  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.

思路:

两种方法:
第一,使用递归,相当于遍历了整个二叉树,递归返回深度浅的那棵子树的深度。
第二,按层检查有没有叶子节点,有的话停止检查,返回当前层数。至于实现这个按层检查,可以用递归的方法。该法不用遍历整个树。
                                                          --来自于牛客426550号
由于我本人只想到第一种办法,看到牛客友人牛客426550号给的思路觉得考虑的比较周全,所以就在此引用一下。谢谢!
递归要分清楚几种情况:
1、当根节点为空时,返回0
2、当只有左子节点(无右子节点)时,返回 1
3、当只有右子节点(无左子节点)时,返回 1
4、当左右子节点都存在时,分别计算左子树和右子树的深度,min(左子树最小深度,右子树最小深度)+1
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】【查找二叉树最小深度】的更多相关文章

  1. Java实现查找二叉树&C++的做法

    写了个Java的查找二叉树,用递归做的,不用递归的还没弄出来.先贴一下.回头再研究. BTreeTest.java: public class BTreeTest{ class Node{ Node ...

  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. LinCode 刷题 之二叉树最小深度

    http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/  题目描述信息 /** * Definition of Tree ...

  4. leetcode-111. 二叉树最小深度 · Tree + 递归

    题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...

  5. LeetCode111_求二叉树最小深度(二叉树问题)

    题目: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the s ...

  6. 剑指offer-第六章面试中的各项能力(二叉树的深度)

    题目:1:输入一个二叉树,求二叉树的深度.从根节点开始最长的路径. 思路:我们可以考虑用递归,求最长的路径实际上就是求根节点的左右子树中较长的一个然后再加上1. 题目2:输入一颗二叉树的根节点,判断该 ...

  7. Java实现 LeetCode 111 二叉树的最小深度

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

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

  9. [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 ...

随机推荐

  1. Web 2D/3d

    首选应该是H5,通过现成的js库来实现,兼容性应该不错 其次可以考虑使用Unity3d,开发起来应该比较快 搜集点资料先放起来~ Unity3d: http://unity3d.com/cn/get- ...

  2. CSS 盒子

    转载自:http://www.zblog.us/programing/web/css/cssbox-introduce-2.html 盒子模型定义 如果CSS对HTML文档元素生成了一个描述该元素在H ...

  3. BUG等级和严重等级关系

  4. Xcode插件描述

    Xcode插件 Xcode是iOS的集成开发环境,虽然苹果一直在不断改进Xcode,但程序员总是有各种新奇的想法和需求,当Xcode无法满足他们时,于是他们就会通过插件的方式来为Xcode增加新的功能 ...

  5. Map三种遍历方式

    Map三种遍历方式 package decorator; import java.util.Collection; import java.util.HashMap; import java.util ...

  6. QDataStream和QByteArray

    一个写操作可以参考: QDataStream &operator >>(QDataStream &in, SerializedMessage &message) { ...

  7. ==与equals 的使用比较

    1. == 是一个运算符. 2.Equals则是string对象的方法 我们通常是两种类型的比较 1.基本数据类型比较 2.引用对象比较 其中 1.基本数据类型比较 ==和Equals都比较两个值是否 ...

  8. jQuery Mobile应用之火车票查询

    效果图: 在CMD中输入如下代码 corsproxy (前提是有node.js环境,并先安装corsproxy) html: <!DOCTYPE html> <html> &l ...

  9. JDBC 内部资料 请勿转载 谢谢合作

    一.JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connect ...

  10. Android的学习第六章(布局一TableLayout)

    今天我们来简单的说一下Android不居中的TableLayout布局(表格布局) 表格布局的意思就是将我们的布局看做为一个表格,主要用于对控件进行整齐排列 我们看一个简单的案例 <TableL ...