Java for 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 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的更多相关文章
- Java实现LeetCode 111. Minimum Depth of Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...
- [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 ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 ...
- 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 ...
- 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 ...
- leetcode 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- (二叉树 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 ...
随机推荐
- php分页显示文章列表
<div class="content"> <ul> <?php $querySel = "select * from news where ...
- C#面试基础知识2
1.C#三层架构 C#三层架构急表示层(UI,User Interface),业务逻辑层(BLL BusinessLogicLayer),数据访问层(DAL Data Access Layer).三层 ...
- Mycat本地模式的自增长分表操作
Mycat对表t_rc_rule_monitor做分表操作 在mysql上执行(没有t_rc_rule_monitor) DROP TABLE IF EXISTS t_rc_rule_monitor; ...
- script脚本中写不写$(document).ready(function() {});的差别
$(document).ready() 里的代码是在页面内容都载入完才运行的,假设把代码直接写到script标签里.当页面载入完这个script标签就会运行里边的代码了,此时假设你标签里运行的代码调用 ...
- 微信小程序(应用号)开发新闻客户端的实战课程
摘要: 本实例将演示从零开发一个微信应用号的过程,页面轮播与跳转传值,实现单元格自定义布局,全部源码可通过github下载. 下载最新版的微信小程序开发工具,目前是v0.9.092300 下载地址:h ...
- 关于 html 中 table 表格 tr,td 的高度和宽度
http://wenku.baidu.com/link?url=76BZcBS3YyA1QJwE7pCPJKERPex4uSQEQ1LI5ZkwTCtunw2cBTaLI8E71dxUhFW0CH4h ...
- ThinkPHP中的模型命名
当我们创建一个UserModel类的时候,其实已经遵循了系统的约定.ThinkPHP要求数据库的表名和模型类的命名遵循一定的规范,首先数据库的表名和字段全部采用小写形式,模型类的命名规则是除去表前缀的 ...
- ORCAD中的一些操作小技巧
1.ORCAD中改变元器件和文本字体颜色的命令: 打开在 View -> Toolbar -> Command Window.然后圈选文字(可复选),然后到 Command Window ...
- C语言日期计算器
记录下码子 # define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <stdlib.h> int days ...
- modelsim-altera
一. 1. Go to the menu Tools > Options. 2. In the “General” category, select “EDA Tool Options”. ...