问题

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.

解决思路

1. 当节点为叶节点时,深度为1;

2. 当节点只有左孩子(或右孩子)时,深度为左孩子(或右孩子)+1;

3. 当节点既有左孩子又有右孩子时, 深度为min(左孩子,右孩子)+1。

代码

 /**
* Definition for binary tree
* 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)
return 0;
if(root.left==null && root.right==null)
return 1;
if(root.left==null && root.right!=null)
return run(root.right)+1;
if(root.left!=null && root.right==null)
return run(root.left)+1;
return Math.min(run(root.left), run(root.right))+1;
}
}

树——minimum-depth-of-binary-tree(二叉树的最小深度)的更多相关文章

  1. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

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

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

  5. [Leetcode] The minimum depth of binary tree二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  6. 111 Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...

  7. LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

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

  9. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  10. N1-1 - 树 - Minimum Depth of Binary Tree

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

随机推荐

  1. Beauty Values

    Beauty Values 题意:给$n$个数, 定义它的Beauty Values为所有连续子区间的(区间长度*区间内不同数字的数目)求和 求Beauty Values A[i]数组表示数字i最近一 ...

  2. 11 November

    Weakness 求数列区间 \(\{a_n\}\) 中满足 \(i < j < k, a_i > a_j > a_k\) 的 \((i, j, k)\) 对的数目. 设对 \ ...

  3. Oracle诊断:使用USER_SEGMENTS分配给表的物理空间大小

    假设我的SCHEMA的名字是abc, 需要知道在这个SCHEMA下的数据容量,可以通过下面的方式. 1.登录SCHEMA abc 2.使用USER_SEGMENTS查看SCHEMA abc数据容量 S ...

  4. 最近使用的两个工具 winscp和xshell

    最近在编译MONO源码时用到了这两个工具,感觉挺好,记录备忘 WINSCP 长这样 用于可视化的查看和操作远程服务器上的文件 xshell用于命令行下操作远程服务器,服务器的同学好多在用这个.命令行是 ...

  5. day03-Python基础

    1:函数 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 函数定义: def sayh ...

  6. Gym 100942A Three seamarks

    题目链接: http://codeforces.com/problemset/gymProblem/100942/A ----------------------------------------- ...

  7. First-order logic

    w https://en.wikipedia.org/wiki/First-order_logic

  8. 《图解设计模式》读书笔记8-2 MEMENTO模式

    目录 Memento模式 示例代码 程序类图 代码 角色和类图 模式类图 角色 思路拓展 接口可见性 保存多少个Memento 划分Caretaker和Originator的意义 Memento模式 ...

  9. altium学习之常用快捷键

    1.放大缩小:常用方法,ctrl+鼠标滚轮,鼠标中键+移动鼠标,pgup.pgup. 2.切换不同的布线层:ctrl+shift+鼠标滚轮 3.在SCH或者PCB 同一平面内左右翻转:ctrl+X 4 ...

  10. 【python+selenium自动化】图像识别技术在UI自动化测试中的实际运用

    引言: 目前在图像识别方面的自动化测试框架有很多,其中比较有名的是airtest,主要做手机端的游戏自动化测试(http://airtest.netease.com/) 因为没有实际把airtest运 ...