LeetCode 111. Minimum Depth of Binary Tree
来源:互联网 发布:淘宝加盟店 编辑:程序博客网 时间:2024/06/11 01:45
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.
叶子节点是没有子节点的节点,刚开始没有理解这句话。所以对于测试用例[1,2],返回了1,应该是2 。 希望大家注意。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { int length = 0; int minLength = 0; 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 1 + Math.min(minDepth(root.left),minDepth(root.right)); }}
时间复杂度O(n), n为节点个数, 空间复杂度为O(1)。
0 0
- 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
- 111. Minimum Depth of Binary Tree LeetCode
- 【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
- 111.[Leetcode]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
- 跟我一起复习C++(1)——对象和类
- iOS 生成静态库.a文件教程
- SQL SERVER2005 复制订阅功能介绍
- iOS 使用CoreData进行数据操作
- linux 安装配置 jdk
- LeetCode 111. Minimum Depth of Binary Tree
- Java并发包:双端阻塞队列(BlockingDeque)
- LPTSTR、LPCSTR、LPCTSTR、LPSTR的来源及意义
- Class文件结构
- 从0开始写一个高性能epoll网络库
- mysql单表自关联查询
- 用 Visual C++ 2015 编译 FreeType 2.6.2
- oracle dg主从切换
- 25个Java机器学习工具&库