4 lines C++ Easy to understand


  • 0
    A

    Use divide and conquer. If left or right return 0, just return max(right, left) to avoid null node problem.

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

Log in to reply
 

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