/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
// Check root
if (root == null) return 0;
// If is leaf, return
if (root.left == null && root.right == null) return 1;
// If having both children
if (root.left != null && root.right != null) return Math.min(minDepth(root.left)+1, minDepth(root.right)+1);
// Only have one child
if (root.left != null) return minDepth(root.left)+1;
else return minDepth(root.right)+1;
}
}
Minimum Depth of Binary Tree
最新推荐文章于 2014-11-06 13:32:07 发布