C++ easy to understand


  • 0
    class Solution{
    public:
    	int findSecondMinimumValue(TreeNode* root){
    		int Min=INT_MAX;
    		int secondMin=-1;
    		inorder(root,Min,secondMin);
    		return secondMin==INT_MAX?-1:secondMin;
    	}
    	void inorder(TreeNode* root,int& Min,int& secondMin){
    		if(root){
    			inorder(root->left,Min,secondMin);
    			if(root->val<Min){
                                    secondMin=Min;
    				Min=root->val;
    			}
                            else if(root->val>Min&&root->val<secondMin){
                                    secondMin=root->val;
                }
    			inorder(root->right,Min,secondMin);
    		}
    	}
    };
    

Log in to reply
 

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