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.
int depth(TreeNode* root, int level) {
if (NULL == root) {
return level - 1;
}
if ((NULL == root->left) && (NULL == root->right)) {
return level;
} else if (NULL == root->left) {
return depth(root->right, level + 1);
} else if (NULL == root->right) {
return depth(root->left, level + 1);
} else {
int l = depth(root->left, level + 1);
int r = depth(root->right, level + 1);
return l < r ? l : r;
}
}
int minDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return depth(root, 1);
}