[Leetcode] Minimum Depth of Binary Tree (Java)
2014-01-20 18:34
369 查看
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.
找到最近叶子节点的高度
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
找到最近叶子节点的高度
public class Solution { public int minDepth(TreeNode root) { if(root==null) return 0; if(root.left==null&&root.right==null) return 1; int left = 0; int right =0; if(root.left!=null) left = minDepth(root.left); if(root.right!=null) right = minDepth(root.right); if(right!=0&&left!=0) return 1+Math.min(left, right); if(right!=0) return 1+right; return 1+left; } }
相关文章推荐
- Java [Leetcode 111]Minimum Depth of Binary Tree
- LeetCode|Minimum Depth of Binary Tree-java
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- [LeetCode][Java] Minimum Depth of Binary Tree
- Java for LeetCode 111 Minimum Depth of Binary Tree
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree leetcode java
- [LeetCode][Java] Minimum Depth of Binary Tree
- 【leetcode】Minimum Depth of Binary Tree 二叉树的最小深度 (JAVA)
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- leetcode:Minimum Depth of Binary Tree 【Java】
- LeetCode OJ Minimum Depth of Binary Tree
- LeetCode 111:Minimum Depth of Binary Tree
- LeetCode Minimum Depth of Binary Tree
- [LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree
- [LeetCode][Java] Maximum Depth of Binary Tree
- LeetCode-Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree--LeetCode
- LeetCode 111 Minimum Depth of Binary Tree