[leetcode]Minimum Depth of Binary Tree

Minimum Depth of Binary Tree

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.

 

[leetcode]Maximum Depth of Binary Tree 差不多。

递归,不多说。

 1 public class Solution {
 2     public int minDepth(TreeNode root) {
 3         if(root == null) return 0;
 4         if(root.left == null && root.right == null) return 1;
 5         int left = Integer.MAX_VALUE,right = Integer.MAX_VALUE;
 6         if(root.left != null) left = minDepth(root.left);
 7         if(root.right != null) right =  minDepth(root.right);
 8         return left > right ? right + 1 : left +1;
 9     }
10 }

 

posted on 2014-07-31 20:06  喵星人与汪星人  阅读(214)  评论(0编辑  收藏  举报