Java for LeetCode 111 Minimum Depth of Binary Tree

时间:2022-08-27 16:33:48

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.

解题思路:注意minimum depth最后遍历的那个点,left right都必须为null,JAVA实现如下:

    public int minDepth(TreeNode root) {
if(root==null)
return 0;
else if(root.left==null)
return minDepth(root.right)+1;
else if(root.right==null)
return minDepth(root.left)+1;
else return Math.min(minDepth(root.left),minDepth(root.right))+1;
}

Java for LeetCode 111 Minimum Depth of Binary Tree的更多相关文章

  1. Java实现LeetCode 111. Minimum Depth of Binary Tree

    /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...

  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 shor ...

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

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

  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 ----- java

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

  6. 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 ...

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

  8. leetcode 111 minimum depth of binary tree

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

  9. (二叉树 BFS DFS) 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 ...

随机推荐

  1. PLSQL win7 64位

    1. 解压instantclient-basic-win32-11.2.0.1.0.zip至Oracle安装目录的Product下 具体目录如下D:\Oracle\product\instantcli ...

  2. leetcode implement strStr python

    #kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...

  3. JS实现数组每次只显示5条数据

    var array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; //循环样式结构function fun(arr,index){ var str = &qu ...

  4. Oracle Inventory Management Application Program Interface ( APIs)

    In this Document   Goal   Solution   References APPLIES TO: Oracle Inventory Management - Version 12 ...

  5. Java基础:JVM垃圾回收算法

    众所周知,Java的垃圾回收是不需要程序员去手动操控的,而是由JVM去完成.本文介绍JVM进行垃圾回收的各种算法. 1. 如何确定某个对象是垃圾 1.1. 引用计数法 1.2. 可达性分析 2. 典型 ...

  6. py3.0第四天 函数

    列表生成 # -*- coding: utf-8 -*- # data =[1,2,3] # for index,i in enumerate(data): # print (index,i) # d ...

  7. Nginx Redirect Websocket

    I want to redirect my websocket to another server. As we known, nginx command rewrite or redirect ca ...

  8. rpgmakermv(8) XY_TitleMenu插件

    插件作用:设置标题 /*: * @plugindesc v1.00 Display Multiple Menu in Title Screen. * @author XueYu Plugins * * ...

  9. asp.net mvc如何获取url的相关信息

    1.获取完整url信息(协议名+域名+虚拟目录名+文件名+参数) string url = Request.Url.ToString(); 如: //1)获取完整url(协议名+域名+虚拟目录名+文件 ...

  10. String的intern()方法详解

    https://blog.csdn.net/soonfly/article/details/70147205  :图解 https://blog.csdn.net/wjzhang5514/articl ...