Problem Minimum Depth of Binary Tree

Problem Description:

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.

Solution:

1     public int minDepth(TreeNode root) {
2         if (root == null) return 0;
3         int left = minDepth(root.left);
4         int right = minDepth(root.right);
5         return 1 + (left == 0 ? right : (right == 0 ? left : Math.min(left, right)));
6     }

 

 

posted @ 2014-06-29 15:46  HaruHaru  阅读(111)  评论(0编辑  收藏  举报