Simple Python BFS, without set()

  • 0
    class Solution(object):
        def findSecondMinimumValue(self, root):
            :type root: TreeNode
            :rtype: int
            if root is None: return -1
            Q = collections.deque()
            first, ret = root.val, sys.maxint
            while Q:
                node = Q.popleft()
                if node.val > first:
                    ret = min(ret, node.val)
                if node.left: Q.append(node.left)
                if node.right: Q.append(node.right)
            return ret if ret != sys.maxint else -1

  • 0

    @Andrinux looks good.

  • 0


    What if the second minimum node is indeed MAX_INT?

  • 0

    python's integer could be extended automatically. There is not a hard integer limit like C++/Java. And here, sys.maxint is 9223372036854775807. It is large enough for almost all applications.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.