题目要求:
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.
代码:class Solution {
public:
int minDepth(TreeNode *root) {
if(root == NULL)
return 0;
int left_deep = minDepth(root->left);
int right_deep = minDepth(root->right);
if(left_deep == 0 && right_deep == 0)
return 1;
if(left_deep == 0)
left_deep = numeric_limits<int>::max();
if(right_deep == 0)
right_deep = numeric_limits<int>::max();
return 1 + ((left_deep > right_deep)? right_deep : left_deep);
}
};