leedcode做题总结,题目Minimum Depth of Binary Tree 2012/10/09
2014-07-15 16:02
387 查看
找二叉树深度的最小值。和求树高差不多,注意的是递归中不能和求树高一样直接返回两个子树的最小值,因为如果一个节点只有一个子树求两边的最小值会出错,所以需要判断下。
public int minDepth(TreeNode root) { if(root==null) return 0; else{ if(root.right==null) return minDepth(root.left)+1; if(root.left==null) return minDepth(root.right)+1; return Math.min(minDepth(root.right)+1,minDepth(root.left)+1); } }
相关文章推荐
- leedcode做题总结,题目Maximum Depth of Binary Tree-------2012/09/29
- LeetCode算法题目:Minimum Depth of Binary Tree
- leedcode做题总结,题目Flatten Binary Tree to Linked List 2012/10/14
- leedcode做题总结,题目Binary Tree Level Order TraversalI/II 2012/09/28
- [leedcode 111] Minimum Depth of Binary Tree
- LeetCode题目笔记(一) -- Minimum Depth Of Binary Tree
- leedcode做题总结,题目Binary Tree Postorder Traversal-------- 2013/11/07
- LeetCode: Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - DFS
- [Leetcode 10] 111 Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- 【leetcode】Minimum Depth of Binary Tree
- LeetCode : Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- [LeetCode]Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- [面试真题] LeetCode:Minimum Depth of Binary Tree