Leetcode 513 Solution

This article provides solution to leetcode question 513 (find-bottom-left-tree-value)

https://leetcode.com/problems/find-bottom-left-tree-value

Solution

# 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 findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        q = collections.deque()

        q.append(root)

        while q:
            left_node = q[0]
            s = len(q)

            for i in range(s):
                node = q.popleft()

                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)

            if not q:
                return left_node.val