Skip to content

Latest commit

 

History

History
30 lines (28 loc) · 839 Bytes

513.md

File metadata and controls

30 lines (28 loc) · 839 Bytes

513. Find Bottom Left Tree Value

Solution 1 (time O(n), space O(1))

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def findBottomLeftValue(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.ans = -1
        self.max_height = -1
        def dfs(cur_node, cur_height):
            if not cur_node:
                return
            dfs(cur_node.left, cur_height + 1)
            dfs(cur_node.right, cur_height + 1)
            if cur_height > self.max_height:
                self.ans = cur_node.val
                self.max_height = cur_height
        dfs(root, 0)
        return self.ans