min1
to root.val
and ans
to float('inf')
.dfs(node)
that will perform the depth-first search.dfs(node)
, if the node is None
, return immediately.node.val
is greater than min1
and less than ans
, update ans
to node.val
.node.val
equals min1
, recursively call dfs
on both node.left
and node.right
.dfs(root)
to start the DFS traversal from the root.ans
is still float('inf')
, return -1
as there is no second minimum value. Otherwise, return ans
.