Java [Leetcode 111]Minimum Depth of Binary Tree
2016-02-04 12:50
519 查看
题目描述:
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) { if(root == null) return 0; if(root.left == null) return 1 + minDepth(root.right); if(root.right == null) return 1 + minDepth(root.left); return Math.min(1 + minDepth(root.left), 1 + minDepth(root.right)); } }
相关文章推荐
- Spring对hibernate的事物管理
- java注解的学习
- Scala学习笔记-1用Eclipse和IDEA搭建Scala+Spark开发环境
- springmvc @ResponseBody 乱码
- 测试产生Java内存异常
- 2016蓝桥杯假期任务之《大小写转换》
- 使用Spring JMS收发消息
- i++和++i是否为原子操作
- [Spring实战系列](9)装配集合
- 整合之道--Spring4整合Hibernate5
- Eclipse SVN 插件 subclipse 安装使用。
- Java 读取多个文件流合并操作
- java Map集合框架之LinkedHashMap
- JAVA反射汇总和示例
- sqoop import ERROR sqoop.Sqoop: Got exception running Sqoop: java.lang.RuntimeExceptrion
- Struts2 文件下载(Stream)
- Spring 面试
- 深入分析Java的序列化与反序列化
- java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/DataSources
- Java课后习题——校验密码