C++ very easy 3ms solution, no set


  • 0
    M
        int first=INT_MAX, sec=INT_MAX;
        int findSecondMinimumValue(TreeNode* root) {
            if(root==NULL||root->left==NULL) return -1;
            DFS(root); 
            if(first==INT_MAX||sec==INT_MAX) return -1;
            return sec;
        }
        
        void DFS(TreeNode* r) {
            if(r->left==NULL&&r->right==NULL) {
                if(r->val!=first&&r->val!=sec&&r->val<sec) sec=r->val;
                if(sec<first) swap(sec, first);
                return;
            }
            DFS(r->left);
            DFS(r->right);
        }

Log in to reply
 

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