Clear C++ solution


  • 0
    Z

    class Solution {
    public:
    int minDepth(TreeNode* root) {
    if(root == NULL){
    return 0;
    }
    if(root->left == NULL && root->right == NULL){
    return 1;
    }
    return helper(root);

    }
    
    int helper(TreeNode* root){
        if(root->left == NULL && root->right == NULL){
            return 1;
        }
        if(root->left == NULL){
            return helper(root->right) + 1;
        }
        if(root->right == NULL){
            return helper(root->left) + 1;
        }
        return 1 + min(helper(root->left), helper(root->right));
    }
    

    };


Log in to reply
 

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