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.

代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution { public int minDepth(TreeNode root) { Stack<Integer> stack=new Stack<>();
int depth=0;
if(root==null)
return 0; if(root.left==null&&root.right==null)
{return 1;} if(root.left!=null)
{
depth=1+minDepth(root.left);
if(stack.empty()||stack.peek()>depth)
stack.push(depth);
} if(root.right!=null)
{
depth=1+minDepth(root.right);
if(stack.empty()||stack.peek()>depth)
stack.push(depth);
} return stack.peek();
}
}

111. Minimum Depth of Binary Tree的更多相关文章

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

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

  2. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  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 111 Minimum Depth of Binary Tree 二叉树

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

  5. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  6. [LeetCode]题解(python):111 Minimum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

  7. leetcode 111 Minimum Depth of Binary Tree ----- java

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

  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. Java [Leetcode 111]Minimum Depth of Binary Tree

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

随机推荐

  1. No suitable driver found for jdbc:mysql://localhost/dbname

    把mysql-connector-java的jar包放入jdk/jre/lib/ext文件下

  2. CF 241E flights 最短路,重复迭代直到稳定 难度:3

    http://codeforces.com/problemset/problem/241/E 首先检测哪些点会出现在从起点到终点的路上,可以用dfs或者迭代, 然后,对于所有的边,设f为边起点,t为边 ...

  3. DataGridView复选框实现单选功能(二)

    双击DataGridView进入事件 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventA ...

  4. 三元运算+lambda表达式

    #三元运算,三目运算,if else简写 if 1 == 1: name = "liangml" else: name = "NB" #如果 1==1 成立,n ...

  5. PhpStorm WebMatrix xDebug 配置开发环境

    1.首先下载WebMatrix安装程序,下载地址 http://www.microsoft.com/web/webmatrix/  安装步骤 参考:http://www.jb51.net/softjc ...

  6. caches 文件夹删除

    模拟器 可以 删除 真机不行

  7. Chrome 开发者工具有了设备模拟器

    今天从哥们那里学到了一个小技巧,使用chrome自带的多设备模拟器来调试页面在不同设备下的显示效果. 特地上网查了一下,记录一下. 如果想要在 Chrome 上测试网站在不同设备,不同分辨率的显示情况 ...

  8. 第一次使用Git心得体会

    用书本上的概念讲,Git是一个分布式的版本控制工具,每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,能够不依赖于网络和中心服务器.也就是说Git能够不需要服务器而在 ...

  9. get( )与getline( )区别

    get与getline区别不是很大,但一个明显的区别是get遇到 '\n '字符后便返回,这是 '\n '还在缓冲区中,所以下次读出来的将是 '\n ',而getline遇到 '\n '也返回,但它会 ...

  10. Android 自动朗读(TTS)

    在Android应用中,有时候需要朗读一些文本内容,今天介绍一下Android系统自带的朗读TextToSpeech(TTS).自动朗读支持可以对指定文本内容进行朗读,还可以把文本对应的音频录制成音频 ...