python solution recursive solution


  • 0
    S

    Instead of doing BFS, do recursive call tossing x and y position. I like this because it only take O(log n) space whereas BFS takes O(n) space.

    class Solution(object):
        def findBottomLeftValue(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            def visit(node, x, y):
                if node == None: return
                if y > visit.y or (y == visit.y and x < visit.x):
                    visit.x, visit.y, visit.bl = x, y, node.val
                visit(node.left, x-1, y+1)
                visit(node.right, x+1, y+1)
            visit.x, visit.y, visit.bl = 0, 0, root.val
            visit(root, 0, 0)
            return visit.bl

Log in to reply
 

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