#671

from utils.treeNode import TreeNode

class Solution:
    def second_minimum(self, root):
        if not root:
            return -1
        
        level = [root]
        curr_min = root.value

        while level:
            next_level = []
            for node in level:
                if node.left:
                    next_level.append(node.left)
                    next_level.append(node.right)
            
            candidates = [node.value for node in next_level if node.value > curr_min]
            if candidates:
                return min(candidates)
            
            level = next_level
        
        return -1


# one = TreeNode(2)
# two = TreeNode(2)
# three = TreeNode(5)
# four = TreeNode(5)
# five = TreeNode(7)

# one.left = two
# one.right = three
# three.left = four
# three.right = five

one = TreeNode(2)
two = TreeNode(2)
three = TreeNode(2)

one.left = two
one.right = three

print(one)

print('=========')
solution = Solution()
print(solution.second_minimum(one))
            