【leetcode】【111】Minimum Depth of Binary Tree
来源:互联网 发布:云计算基地建设成本 编辑:程序博客网 时间:2024/05/16 05:14
一、问题描述
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.
二、问题分析
递归解法,判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth(自己稍微画几个树就可以理解了)。
三、Java AC 代码
public int minDepth(TreeNode root) {return minLev(root);}public int minLev(TreeNode node){if (node==null) {return 0;}int leftLev = minLev(node.left);int rightLev = minLev(node.right);if (leftLev==0) {return rightLev+1;}if (rightLev==0) {return leftLev+1;}return Math.min(leftLev, rightLev) + 1;}
0 0
- LeetCode(111)Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree [111]
- LeetCode 111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- [LeetCode 111]Minimum Depth of Binary Tree
- leetCode #111 Minimum Depth of Binary Tree
- leetcode-111 Minimum Depth of Binary Tree
- leetcode 111 Minimum Depth of Binary Tree
- leetcode || 111、Minimum Depth of Binary Tree
- Leetcode[111]-Minimum Depth of Binary Tree
- leetcode[111]:Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- leetcode-111-Minimum Depth of Binary Tree
- Leetcode #111 Minimum Depth of Binary Tree
- leetcode 111: Minimum Depth of Binary Tree
- LeetCode 111: Minimum Depth of Binary Tree
- 【LEETCODE】111-Minimum Depth of Binary Tree
- iOS9适配之应用跳转(URLSchemes)
- Shell编程——正则表达式基础与扩展
- FFmpeg中的时间戳(DTS和PTS)
- VLC 插件 安装
- centOS下yum安装配置samba
- 【leetcode】【111】Minimum Depth of Binary Tree
- C#中值类型和引用类型的区别
- C语言string家族函数使用说明及范例
- M_BUS总线技术概述
- MongoDB小结27 - 聚合管道【$project】
- 7.4 E2D3
- JAVA连接各类数据库及连接EXCEL代码
- 最大公约数,最小公倍数
- 提高效率的 Android Studio 技巧汇总