I've done my fair share of reading and researching but I still don't 100% get it. For this solution of " Minimum Depth of Binary Tree", the idea of having multiple returns in a recursive function is killing me. I'm not exactly sure how the value for the "minimum depth" is being incremented, and I understand it may have something to do with my misunderstanding of return statements work. Please help, thank you.
int minDepth(Node *root) {
if(!root) return 0;
if(!root->left) return 1 + minDepth(root->right);
if(!root->right) return 1 + minDepth(root->left);
return 1+min(minDepth(root->left),minDepth(root->right));
}
If it helps, logically, what you have above could just as well have been written
int minDepth(Node *root) {
int result;
if(!root)
result = 0;
else if(!root->left)
result = 1 + minDepth(root->right);
else if(!root->right)
result = 1 + minDepth(root->left);
else
result = 1 + min(minDepth(root->left), minDepth(root->right));
return result;
}