leetcode:Minimum Depth of Binary Tree 【Java】
2016-03-07 16:00
393 查看
一、问题描述
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.
二、问题分析
利用迭代思想。
三、算法代码
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) { return minDepth(root, false); } public int minDepth(TreeNode root, boolean hasbrother) { if (root == null) return hasbrother ? Integer.MAX_VALUE : 0; return 1 + Math.min(minDepth(root.left, root.right != null), minDepth(root.right, root.left != null)); } }
相关文章推荐
- java基本算法排序
- java字符串加密解密
- java 使用Reader和Writer对文件内容的拷贝
- JAVA_OPTS参数说明与配置
- JAVA字符串类型-String.format()函数用法
- java内存模型
- 【java小工具】从.java文件中筛选出方法,并计算出方法在这些文件中出现的次数
- 【集合框架】JDK1.8源码分析之HashMap & LinkedHashMap迭代器(三)
- leetcode:Maximum Depth of Binary Tree 【Java】
- 查看Eclipse版本号,及各个版本区别
- Spring MVC之@RequestBody, @ResponseBody 详解
- [springmvc]返回ModelAndView,视图解析不到jsp,而是去解析“'请求路径'+ ‘.jsp’”
- Bug:java.lang.IllegalStateException
- osgi与流行的框架(spring,struts,hibernate等)的集成
- Eclipse中ndk配置及示例
- spring 定时器配置
- Springmvc 执行流程
- leetcode:Balanced Binary Tree 【Java】
- java中Scanner类读取文件或控制台输入
- Struts+Spring+Hibernate处理Lob(Blob,Clob)