1. Code
class Solution:
def deepestLeavesSum(self, root: TreeNode) -> int:
def dfs(node, cnt):
# store the depth and value of a leaf node
if not node.left and not node.right:
self.deep.append(cnt)
self.values.append(node.val)
if node.left:
dfs(node.left, cnt+1)
if node.right:
dfs(node.right, cnt+1)
self.deep, self.values = [], []
sum = 0
dfs(root, 0)
# Find the depth of the deepest and add values of the deepest nodes.
maxdep = max(self.deep)
for i in range(len(self.deep)):
if self.deep[i] == maxdep:
sum += self.values[i]
return sum
2. Result
Runtime : 92 ms(69.82%), Memory usage : 17.6 MB(82.42%)
(Runtime can be different by a system even if it is a same code.)