Find Bottom Left Tree Value LeetCode solution in Python
1# Definition for a binary tree node.2# class TreeNode:3# def __init__(self, val=0, left=None, right=None):4# self.val = val5# self.left = left6# self.right = right7class Solution:8def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:910res = root.val11stack = [(0, root)]12prev_d = 01314while stack:1516curr_d, curr_v = stack.pop(0)1718if curr_v.left:19stack.append((curr_d + 1, curr_v.left))20if prev_d != curr_d + 1:21res = curr_v.left.val22prev_d = curr_d + 12324if curr_v.right:25stack.append((curr_d + 1, curr_v.right))26if prev_d != curr_d + 1:27res = curr_v.right.val28prev_d = curr_d + 12930return res313233# An Upvote will be encouraging