Based on the special property of the tree, we can guarantee that the root node is the smallest node in the tree. We just have to recursively traverse the tree and find a node that is bigger than the root node but smaller than any existing node we have come across.
class Solution(object): def findSecondMinimumValue(self, root): res = [float('inf')] def traverse(node): if not node: return if root.val < node.val < res: res = node.val traverse(node.left) traverse(node.right) traverse(root) return -1 if res == float('inf') else res
@keyanp I think it is because in Python 2 you cannot modify references to variables within functions outside of its scope. By wrapping the value in a list, it allows modification of the value without modifying the reference to the list variable. Alternatively you can use an instance variable like
self.res = float('inf').
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.