9ms c++ solution


  • 0
    P
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int minDepth(TreeNode* root) {
            TreeNode dummy(0);dummy.left = root;
            return helper(&dummy) - 1;
        }
        int helper(TreeNode* root) {
            if(root && !root->left && !root->right)return 1;
            if(!root) return INT_MAX;
            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.