My C Solution


  • 0
    A
    #define min(a,b) ((a) < (b) ? (a) : (b))
    
    int minDepth(struct TreeNode* root) {
        if (root == NULL)
            return 0;
        int l = 0, r = 0;
        if (root->left) l = minDepth(root->left);
        if (root->right) r = minDepth(root->right);
        
        if (l == 0 && r == 0)
            return 1;
        else if (l != 0 && r != 0)
            return 1 + min(l, r);
        else
            return l + r + 1;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.