Simple Python BFS, without set()


  • 0
    A
    class Solution(object):
        def findSecondMinimumValue(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if root is None: return -1
            Q = collections.deque()
            Q.append(root)
            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
    G

    @Andrinux looks good.


  • 0
    V

    @Andrinux

    What if the second minimum node is indeed MAX_INT?


  • 0
    A

    @vonzcy
    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.