Easy to understand level order traversal (Python) (beats 90%)


  • 0
    T
    def findBottomLeftValue(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        prev_level = 0
        q = [(root, 1)]
        while len(q):
            node, level = q.pop(0)
            if level > prev_level:
                result = node.val
                prev_level = level
            if node.left:
                q.append((node.left, level + 1))
            if node.right:
                q.append((node.right, level + 1))
        return result

Log in to reply
 

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