Iterative BFS with Prioritization Trick


  • 0
    J

    Left-to-right approach

    class Solution(object):
        def findBottomLeftValue(self, root):
            ans = root
            lvl = [root]
            while lvl:
                ans = lvl[0]
                lvl = [c for n in lvl for c in [n.left, n.right] if c]
            return ans.val
    

    Right-to-left approach

    class Solution(object):
        def findBottomLeftValue(self, root):
            queue = [root]
            for node in queue:
                queue += filter(None, (node.right, node.left))
            return node.val
    

Log in to reply
 

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