Accepted C++ recursive solution


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

Log in to reply
 

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