111. Minimum Depth of Binary Tree

最大深度を求めるより全然難しい。左右どちらかが、なければもう一方を探索していく。

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root)
        {
            return 0;
        }
        
        if (root->left == NULL) {
            return minDepth(root->right) +1 ;
        }
        if (root->right == NULL) {
            return minDepth(root->left) +1;
        }

        
        return min(minDepth(root->left), minDepth(root->right))+1;
    }
};