### describe

Given the root of a binary tree, return the sum of values of its deepest leaves.

Example 1:

Input: root = [1, 2, 3, 4, 5, null, 6, 7, null, null, null, null, 8] Output: 15Copy the code

Example 2:

Input: root = [6,7,8,2,7,1,3,9, null, 1, 4, null, null, null, 5] Output: 19Copy the code

Note:

The number of nodes in the tree is in the range [1, 104].
1 <= Node.val <= 100
Copy the code

### parsing

Key = depth, value = values of all nodes in the tree at that depth. DFS traverses the tree to get res, and then computs the sum of the last layer.

class Solution(object):
def deepestLeavesSum(self, root):
"""
:type root: TreeNode
:rtype: int
"""
res = {}
def lastLevel(root, res, count):
if not root:
return
if count not in res:
res[count] = []
res[count].append(root.val)
if root.left:
lastLevel(root.left, res, count + 1)
if root.right:
lastLevel(root.right, res, count + 1)
lastLevel(root, res, 0)
return sum(res[max(res.keys())])

Copy the code

### The results

For the submission of Python online submissions. Memory Usage: For each node in the Python online submission system.Copy the code