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. [原]Nginx+Lua服务端合并静态文件

    http://homeway.me 0x01.About 源代码已经上传到github:https://github.com/grasses/nginx-lua-static-merger nginx ...

  2. RGBA与半透明背景

    概念 所谓RGBA颜色,就是RGB三原色加ALPHA.在给背景加入颜色的同一时候.提供透明度特性. 用法 background:rgba(90,90, 54, 0.5); 支持情况 Firefox 3 ...

  3. GS与MS之间通信

    GS与MS之间通信 注意GS与MS是两个线程,现在是每个map一个线程,他们之间是内部协议进行通信的,那既然是两个线程那如何通信呢,看了net进程通信这个就比较简单了 举个例子 m_pMap-> ...

  4. POJ1942

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24236   Accepted: 6006 ...

  5. zipkin环境搭建

    基于jdk1.8 下载zipkin jar包 用wget下载zipkin官方最新jar包 wget -O zipkin.jar 'https://search.maven.org/remote_con ...

  6. SpringMVC spring-servlet.xml配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  7. Java程序发送邮件

    之前上网有看到过别人总结的使用java程序发送邮件,于是自己下来练习,把自己学习的一些心得总结出来. 首先我们这里需要采用两个jar包: 需要的朋友可以自行上网去CSDN类似的网站上面找 顺便把自己测 ...

  8. 简易bootloader重定位问题

           单板选择NandFlash启动,则硬件上电后,系统会自己主动将NandFlash中的前4K内容复制到STEPSTONE即4K SRAM中.然后从SRAM中的0X0地址启动. 基于mini ...

  9. 关于 IN UPDATE TASK

    [转 http://blog.sina.com.cn/s/blog_6f74e6d50100sq57.html]更新程序必须用一个特殊的FM(update module)来实现. 1.Exportin ...

  10. TCP标准模板

    伪代码 #创建一个TCP服务器 ss = socket() #创建服务器套接字 ss.bind() #把地址绑定到套接字上 ss.listen() #监听连接 inf_loop: #服务器无线循环 c ...