This is the counterpart of finding the height of a binary tree recursively. We start with a depth of 0, and only if a node has 2 children, we add the minimum of their depths.

static int min(int a, int b)
{
if (a <= b) {
return a;
}
return b;
}
unsigned int binarytree_minimum_depth_recursive(const btnode *node)
{
unsigned int depth = 0;
if (node->left && node->right) {
depth = min(binarytree_minimum_depth_recursive(node->left),
binarytree_minimum_depth_recursive(node->right)) + 1;
}
return depth;
}
unsigned int binarytree_minimum_depth(const binarytree *tree)
{
unsigned int depth = 0;
if (tree->root) {
depth = binarytree_minimum_depth_recursive(tree->root);
}
return depth;
}

## Related