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) {
        // TODO
        return 0;
    }

    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
    }
}

@@ -348,32 +348,990 @@
     
-// TODO%0A        return 0
+FirstSecondMinimum fsm = new FirstSecondMinimum();%0A        findSecondMinimumValue(root, fsm);%0A        return (fsm.secondMin == null %7C%7C fsm.firstMin == fsm.secondMin) ? -1 : fsm.secondMin;%0A    %7D%0A%0A    private static class FirstSecondMinimum %7B%0A        private Integer firstMin;%0A        private Integer secondMin;%0A    %7D%0A%0A    private static void findSecondMinimumValue(TreeNode node, FirstSecondMinimum fsm) %7B%0A        if (node == null) %7B%0A            return;%0A        %7D%0A        if (fsm.firstMin == null) %7B%0A            fsm.firstMin = node.val;%0A        %7D else if (fsm.secondMin == null) %7B%0A            fsm.secondMin = node.val;%0A        %7D else %7B%0A            if (fsm.firstMin %3C node.val) %7B%0A                fsm.secondMin = fsm.firstMin;%0A                fsm.firstMin = node.val;%0A            %7D else if (fsm.secondMin %3C node.val) %7B%0A                fsm.secondMin = fsm.secondMin;%0A            %7D%0A        %7D%0A        findSecondMinimumValue(node.left, fsm);%0A        findSecondMinimumValue(node.right, fsm)
 ;%0A  
@@ -1796,32 +1796,32 @@
 ew TreeNode(2);%0A
-
         System.o
@@ -1872,16 +1872,378 @@
 ); // -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.findSecondMinimumValue(root)); // 5
 %0A    %7D%0A%7D

Update problem 671