C++ solution, DFS by recursion


  • 0
    1
    int min=-1;
    int depth=1;
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if(!root)  return 0;
        dfs(root);
        return min;
    }
    void dfs(TreeNode* root){
        if(!root->left and !root->right){
            if(min==-1)
               min=depth;
            else{
                if(min>depth)
                   min=depth;
            }
        }
        else{
            ++depth;
            if(root->left)
               dfs(root->left);
            if(root->right)
               dfs(root->right);
            --depth;
        }
    }

Log in to reply
 

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