C++ solution


  • 0
    E
    class Solution {
    public:
        int findSecondMinimumValue(TreeNode* root) {
                    collect(root);
    		if(nums.size() == 1){
    			return -1;
    		}
    		vector<int> arr(nums.begin(), nums.end());
    		sort(arr.begin(), arr.end());
    		return arr[1];
        }
    private:
    	void collect(TreeNode *root){
    		nums.insert(root->val);
    		if(root->left)collect(root->left);
    		if(root->right)collect(root->right);
    	}
    	set<int> nums;
    };
    
    

  • 0
    E

    In fact I need not to use vector... *(nums.begin() + 1) is OK


Log in to reply
 

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