Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
571 B
3
Indexable
Never
class Solution:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        def traverse(node, height):
            if not node:
                return (None, height)

            l, lheight = traverse(node.left, height+1)
            r, rheight = traverse(node.right, height+1)

            if lheight > rheight:
                return (l, lheight)
            elif rheight > lheight:
                return (r, rheight)
            else:
                return (l, max(lheight, rheight))

        result, height = traverse(root, 0)
        return result
Leave a Comment