package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class _671 {
    public static class Solution1 {
        public int findSecondMinimumValue(TreeNode root) {
            if (root == null) {
                return -1;
            }
            Set<Integer> set = new TreeSet<>();
            dfs(root, set);
            Iterator<Integer> iterator = set.iterator();
            int count = 0;
            while (iterator.hasNext()) {
                count++;
                int result = iterator.next();
                if (count == 2) {
                    return result;
                }
            }
            return -1;
        }

        private void dfs(TreeNode root, Set<Integer> set) {
            if (root == null) {
                return;
            }
            set.add(root.val);
            dfs(root.left, set);
            dfs(root.right, set);
            return;
        }
    }
}

@@ -285,81 +285,12 @@
     
-if (root == null) %7B%0A                return -1;%0A            %7D%0A            
+Tree
 Set%3C
@@ -365,314 +365,134 @@
     
-Iterator%3CInteger%3E iterator = set.iterator();%0A            int count = 0;%0A            while (iterator.hasNext()) %7B%0A                count++;%0A                int result = iterator.next();%0A                if (count == 2) %7B%0A                    return result;%0A                %7D%0A            %7D%0A            return -1
+if (set.size() %3C 2) %7B%0A                return -1;%0A            %7D%0A            set.pollFirst();%0A            return set.pollFirst()
 ;%0A  
@@ -540,16 +540,20 @@
 e root, 
+Tree
 Set%3CInte
@@ -744,29 +744,10 @@
     
-    return;%0A        
 %7D
+%0A
 %0A   

refactor 671