LeetCode(111) Minimum Depth of Binary Tree解题报告
2015-12-13 13:30
525 查看
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.
解题思路:
递归求解,如果有比min还小的深度,就更新min,需要注意的是,最小深度是叶子节点到根节点的深度。如果左子树或右子树为空,那么这个就不能计算在内。
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
解题思路:
递归求解,如果有比min还小的深度,就更新min,需要注意的是,最小深度是叶子节点到根节点的深度。如果左子树或右子树为空,那么这个就不能计算在内。
public class Solution { int min; public int minDepth(TreeNode root) { min = Integer.MAX_VALUE; DepthOfTree(root,0); return min; } public void DepthOfTree(TreeNode root,int depth){ if(root == null){ if(min > depth) min = depth; return ; } if(root.left == null) DepthOfTree(root.right,depth+1); else if(root.right == null) DepthOfTree(root.left,depth+1); else{ DepthOfTree(root.left,depth+1); DepthOfTree(root.right,depth+1); } } }
相关文章推荐
- 6. wordpress plug-in
- HDU5596/BestCoder Round #66 (div.2) 二分BIT/贪心
- SharePoint 2013+ Sqlserver 2014 Kerberos 配置传奇, 最终的解决方案 验证。
- 单片机——我的第一个程序
- Ajax.BeginForm 上传文件
- scala并发编程第4章习题
- 通过wireshark,以及python代码收发邮件,了解smtp协议,pop协议工作过程
- 模板顺序表实现
- 零基础入门学习Python(9):序列
- 升级笔记本bios
- 链表中倒数第k个结点
- 8张图理解Java
- [ACM]环形
- 北京交大yum
- tuzicms二级导航栏目地址URL问题bug修复包
- Mac OS X brew安装php7
- HDU 5596/BestCoder Round #66 (div.2) GTW likes math 签到
- java:IO流学习小结
- EasyDarwin EasyClient中DirectShow采集音视频流程及几种采集方式介绍
- OpenStack CEPH Liberty 统一存储 bug解决