Join me to stay up-to-date and get my new articles delivered to your inbox by subscribing here.
Find the minimum depth (depth of first leaf) of a binary tree.
struct node
{
node *left, *right;
int val;
};
int min_depth(node* root)
{
if (root == NULL) return 0;
if (root->left == NULL) return min_depth(root->right) + 1;
if (root->right == NULL) return min_depth(root->left) + 1;
return 1 + min(min_depth(root->left), min_depth(root->right));
}