Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
936 B
0
Indexable
Never
# 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()
        t = "#"
        level = collections.deque()
        if not root:
            return None
        
        q.append(root)
        q.append(t)
        while q:
            cur = q.popleft()
            if cur == t:
                if len(q) == 0:
                    return level.popleft()
                else:
                    q.append(t)
                    level.clear()
            else:
                level.append(cur.val)
                if cur.left:
                    q.append(cur.left)
                if cur.right:
                    q.append(cur.right)
        return None # should not get here
        
Leave a Comment