Python DFS Clean Solution


  • 0
    D
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def findBottomLeftValue(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            r = [None, 0] # [val, depth]
            def search(node, depth):
                if not node: return
                if depth >= r[1]: 
                    r[0] = node.val
                    r[1] = depth
                search(node.right, depth + 1)
                search(node.left, depth + 1)
            search(root, 1)
            return r[0]

Log in to reply
 

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