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. CSS最常用和实用的技巧

    1.重置浏览器的字体大小重置浏览器的默认值 ,然后重设浏览器的字体大小你可以使用雅虎的用户界面重置的CSS方案 ,如果你不想下载9MB的文件,代码如下: body,div,dl,dt,dd,ul,ol ...

  2. iOS不显示状态栏(电池和信号栏)

    //隐藏状态栏 - (BOOL)prefersStatusBarHidden { return YES; } 在viewcontroller里面加入

  3. 一个socket发送调试信息的类

    using UnityEngine; using System.Collections; using System; using System.Net.Sockets; using System.Ne ...

  4. POJ 2262 Goldbach's Conjecture 数学常识 难度:0

    题目链接:http://poj.org/problem?id=2262 哥德巴赫猜想肯定是正确的 思路: 筛出n范围内的所有奇质数,对每组数据试过一遍即可, 为满足b-a取最大,a取最小 时空复杂度分 ...

  5. ubuntu 14.04 上安装有道词典

    Ubuntu 14.04用户在安装前要更新系统,即update&dist-upgrade. 下载地址:32/64bits http://codown.youdao.com/cidian/lin ...

  6. iOS程序的启动过程-UIWindow

    UIApplicationMain main函数中执行了一个UIApplicationMain这个函数 int UIApplicationMain(int argc, char *argv[], NS ...

  7. 使用eclipse创建在myeclipse中运行的web工程

    今天在跟随慕课网学习java时,遇到课程中老师使用Myeclipse,我用的是eclipse,那么就使用eclipse创建在Myeclipse项目 参考: 如何在Eclipse配置Tomcat服务器 ...

  8. Android 之 数据存储

    在Android操作系统中,提供了5种数据存储方式:SharedPreferences存储,文件存储,SQLite数据库存储,ContentProvider存储和网络存储. 一.SharedPrefe ...

  9. poj2193

    //Accepted 368K 532MS //线性dp //dp[i][j]表示前i位最后一个是j的排列数 //dp[i][j]=sum(dp[i-1][h]) h*2<=j #include ...

  10. treap模版代码

    treap模版暂存. 以后修改整理. #include<cstdio> #include<iostream> #include <time.h> #include& ...