My neat C++ solution


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

  • -1
    L

    int minDepth(TreeNode* root) {
    if(root==NULL) return 0;
    if(root->left==NULL&&root->right==NULL) return 1;
    int left=INT_MAX,right=INT_MAX;
    if(root->left!=NULL)
    left=minDepth(root->left);
    if(root->right!=NULL)
    right=minDepth(root->right);
    int path=min(left,right)+1;
    }


Log in to reply
 

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