Given the <code>root</code> of a binary tree, return <em>the sum of values of its deepest leaves</em>.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/07/31/1483_ex1.png" style="width: 273px; height: 265px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
<strong>Output:</strong> 15
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
<strong>Output:</strong> 19
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 100</code></li>
</ul>

# 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 deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
        
        """
        use a dictionary to store val in their levels
        """

        if not root.left and not root.right:
            return root.val

        queue = deque()
        queue.append((root, 1))
        lvls = {}

        while queue:

            r, lv = queue.popleft()

            if not r:
                continue
            if not r.left and not r.right:
                if lv not in lvls:
                    lvls[lv] = r.val
                else:
                    lvls[lv] += r.val
            if r.left:
                queue.append((r.left, lv + 1))
            if r.right:
                queue.append((r.right, lv + 1))

        maxLv = 0
        lastSum = 0
        for k, v in lvls.items():
            if maxLv < k:
                lastSum = v
                maxLv = k
        
        return lastSum
                
19100