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.


题解:递归解决,用私有变量minDep保存最小的深度,每当遍历到叶节点的时候就看是否需要更新。

代码如下:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int minDep = Integer.MAX_VALUE;
public void minDepthRecur(TreeNode root,int depth){
if(root == null)
return;
if(root.left == null && root.right == null){
if(minDep > depth)
minDep = depth;
return;
}
minDepthRecur(root.left, depth+1);
minDepthRecur(root.right, depth+1);
}
public int minDepth(TreeNode root) {
if(root == null)
return 0;
minDepthRecur(root, 1);
return minDep;
}
}

【leetcode刷题笔记】Minimum Depth of Binary Tree的更多相关文章

  1. [刷题] 104 Maximum Depth of Binary Tree

    要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...

  2. 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 s ...

  3. 力扣算法题—111.Minimum Depth of Binary Tree

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

  4. 【leetcode❤python】 111. Minimum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  5. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  6. leetcode刷题-559. Maximum Depth of N-ary Tree

    题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...

  7. [leetcode刷题笔记]Implement Trie (Prefix Tree)

    题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...

  8. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

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

  9. 【LeetCode练习题】Minimum Depth of Binary Tree

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

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

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

随机推荐

  1. mysql5.5.30源码安装及主从搭建

    双机热备(实验环境) 主服务器:ip地址192.168.100.244,mysql版本5.5.30,源码安装 从服务器:ip地址192.168.100.245 一.源码安装mysql5.5 启动目录: ...

  2. ios8 一些运行问题

     iOS10相册相机闪退bughttp://www.jianshu.com/p/5085430b029fiOS 10 因苹果健康导致闪退 crashhttp://www.jianshu.com/p/5 ...

  3. 经过两个多月的攻关,终于搞定了live555多线程并稳定压测通过

    live555已经发展了十几年了,不得不钦佩作者坚持不懈的奉献和国外的开源生态环境,live555可以说是大部分的安防从业者的入门之选,尤其是在嵌入式或者Linux系统上,其应用还是蛮广泛的,主要是其 ...

  4. 九度OJ 1181:遍历链表 (链表、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2733 解决:1181 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1<=n<=1 ...

  5. Powerdesigner Name与Comment的互相转换

    使用说明: 在[Tools]-[Execute Commands]-[Edit/Run Script] 下.输入下面你要选择的语句即可: 1.Name填充Comment '把pd中那么name想自动添 ...

  6. python中TCP和UDP区别

    TCP(Transmission Control Protocol)可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存&接收缓存).面向字节流.使用TCP的应用:Web浏览 ...

  7. dva+antd写的一个react例子

    github地址 https://github.com/shenggen1987/dva-cli-demo 效果图片

  8. java基础入门之九九乘法表

    /* 自学java 九九乘法表 Power by Stuart Date: 2015.4.23 */public class Math { public static void main (Strin ...

  9. Springmvc注解启用

      http://www.blogbus.com/wanping-logs/235898637.html 使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用 ...

  10. 遇到IIS configuration error错误的可以看看,不一定是权限问题

    最近接手了别人的一个 DOT NET项目,编译.调试一切都OK(心里暗暗高兴),发布吧,结果放到服务器上一运行出现Configuration Error错误,提示:“Access to the pat ...