12 lines c code ,6ms


  • 1
    8
    int mindep;
    void findMinDeep(struct TreeNode *p,int d){
        if(!p->left&&!p->right&&d<mindep) mindep = d;
        if(p->left&&d<mindep) findMinDeep(p->left,d+1);
        if(p->right&&d<mindep) findMinDeep(p->right,d+1);
    }
    int minDepth(struct TreeNode *root) {
        if(!root) return 0;
        mindep = 0x7fffffff;
        findMinDeep(root,1);
        return mindep;
    }

Log in to reply
 

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