Created
February 28, 2024 15:59
-
-
Save braddotcoffee/17410f247f77e5d1cf0b6c90b5e4f5a1 to your computer and use it in GitHub Desktop.
513. Find Bottom Left Tree Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, val=0, left=None, right=None): | |
# self.val = val | |
# self.left = left | |
# self.right = right | |
class Solution: | |
def _left_first_traverse(self, node, depth): | |
if node is None: | |
return | |
if depth > self.max_depth: | |
self.max_depth = depth | |
self.result_node = node | |
# Recurse left side first to ensure the | |
# leftmost node is found first at a new depth | |
self._left_first_traverse(node.left, depth + 1) | |
self._left_first_traverse(node.right, depth + 1) | |
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: | |
self.max_depth = 0 | |
self.result_node = None | |
self._left_first_traverse(root, 1) | |
return self.result_node.val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment