10 lines of c++ solution / 16 ms


  • 1
    P
         int minDepth(TreeNode *root) {
            return root==NULL? 0 : helper(root,0);
         }
        
        int helper(TreeNode* n, int d) {
            if (n == NULL) return INT_MAX;
            if (!n->left && !n->right) return d+1;
            int l = helper(n->left, d+1);
            int r = helper(n->right, d+1);
            return min(l,r);
        }

Log in to reply
 

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