How to optimize this simple solution further?


  • 0
    R

    Not sure how can I optimize this simple solution further. I got the run time as somewhere around 20% more than all the accepted solutions, so there surely should be scope. Please review and help.

    int minDepth(TreeNode* root) {
        if (root == NULL)
            return 0;
        
        int lftDepth = minDepth(root->left);
        int rtDepth = minDepth(root->right);
        
        if (lftDepth != 0 && rtDepth != 0)
        {
            if (lftDepth < rtDepth)
                return lftDepth + 1;
            else
                return rtDepth + 1;            
        }
        else if (lftDepth == 0)
            return rtDepth + 1;
        else
            return lftDepth + 1;
    }

Log in to reply
 

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