Python BFS


  • 0
    B
    class Solution(object):
        def largestValues(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            retVal = []
            if root is None:
                return retVal
            queue = []
            queue.append(root)
            queue.append(float('Inf'))
            while len(queue) > 1 :
                levelarray = []
                while queue[0] != float('Inf'):
                    node = queue.pop(0)
                    levelarray.append(node.val)
                    if node.right:
                        queue.append(node.right)
                    if node.left:
                        queue.append(node.left)
                retVal.append(max(levelarray))
                queue.append(float('Inf'))
                queue.pop(0)
            return retVal
    

Log in to reply
 

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