package leetcode;

/**
 * https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
 */
public class Problem671 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int findSecondMinimumValue(TreeNode root) {
        FirstSecondMinimum fsm = new FirstSecondMinimum();
        findSecondMinimumValue(root, fsm);
        return (fsm.secondMin == null || fsm.firstMin == fsm.secondMin) ? -1 : fsm.secondMin;
    }

    private static class FirstSecondMinimum {
        private Integer firstMin;
        private Integer secondMin;
    }

    private static void findSecondMinimumValue(TreeNode node, FirstSecondMinimum fsm) {
        if (node == null) {
            return;
        }
        if (fsm.firstMin == null) {
            fsm.firstMin = node.val;
        } else if (fsm.secondMin == null) {
            fsm.secondMin = node.val;
        } else {
            if (fsm.firstMin < node.val) {
                fsm.secondMin = fsm.firstMin;
                fsm.firstMin = node.val;
            } else if (fsm.secondMin < node.val) {
                fsm.secondMin = fsm.secondMin;
            }
        }
        findSecondMinimumValue(node.left, fsm);
        findSecondMinimumValue(node.right, fsm);
    }

    public static void main(String[] args) {
        Problem671 prob = new Problem671();

        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(2);
        root.right = new TreeNode(5);
        root.right.left = new TreeNode(5);
        root.right.right = new TreeNode(7);
        System.out.println(prob.findSecondMinimumValue(root)); // 5

        root = new TreeNode(2);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        System.out.println(prob.findSecondMinimumValue(root)); // -1

        root = new TreeNode(2);
        root.left = new TreeNode(2);
        root.right = new TreeNode(5);
        root.right.left = new TreeNode(5);
        root.right.right = new TreeNode(7);
        root.right.right.left = new TreeNode(8);
        root.right.right.right = new TreeNode(8);
        System.out.println(prob.findSecondMinimumValue(root)); // 5
    }
}

@@ -912,32 +912,60 @@
 econdMin == null
+ && fsm.firstMin != node.val
 ) %7B%0A            
@@ -1040,37 +1040,18 @@
 Min 
-%3C node.val) %7B%0A               
+!= null &&
  fsm
@@ -1065,23 +1065,18 @@
 Min 
+!
 = 
-fsm.firstMin;
+null) %7B
 %0A   
@@ -1080,32 +1080,36 @@
                 
+if (
 fsm.firstMin = n
@@ -1097,33 +1097,33 @@
 f (fsm.firstMin 
-=
+%3E
  node.val;%0A     
@@ -1119,61 +1119,16 @@
 .val
-;%0A    
+) %7B%0A
     
-    %7D else if (fsm.secondMin %3C node.val) %7B%0A
     
@@ -1155,30 +1155,29 @@
 ndMin = fsm.
-second
+first
 Min;%0A       
@@ -1181,26 +1181,24 @@
         
-%7D%0A
         
 %7D%0A      
@@ -1193,978 +1193,260 @@
     
-%7D%0A        findSecondMinimumValue(node.left, fsm);%0A        findSecondMinimumValue(node.right, fsm);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem671 prob = new Problem671();%0A%0A        TreeNode root = new TreeNode(2);%0A        root.left = new TreeNode(2);%0A        root.right = new TreeNode(5);%0A        root.right.left = new TreeNode(5);%0A        root.right.right = new TreeNode(7);%0A        System.out.println(prob.findSecondMinimumValue(root)); // 5%0A%0A        root = new TreeNode(2);%0A
+fsm.firstMin = node.val;%0A                %7D else if (fsm.secondMin %3E node.val && fsm.firstMin != node.val) %7B%0A                    fsm.secondMin = node.val;%0A             
    
+%7D%0A
      
-root.left = new TreeNode(2);%0A
+   
     
+%7D%0A
     
-root.right = new TreeNode(2);%0A        System.out.println(prob.findSecondMinimumValue(root)); // -1%0A%0A        root = new TreeNode(2);%0A        root.left = new TreeNode(2);%0A        root.right = new TreeNode(5);%0A        root.right.left = new TreeNode(5);%0A        root.right.right = new TreeNode(7);%0A        root.right.right.left = new TreeNode(8);%0A        root.right.right.right = new TreeNode(8);%0A        System.out.println(prob.
+    %7D%0A        findSecondMinimumValue(node.left, fsm);%0A        
 find
@@ -1468,20 +1468,25 @@
 lue(
-root)); // 5
+node.right, fsm);
 %0A   

Solve problem 671