1
# Definition for a binary tree node.2
# class TreeNode(object):3
# def __init__(self, val=0, left=None, right=None):10
1) find the height of the tree, this way we would know how deep we need to go.11
2) we need a counter to check how deep we are and this is not available in deepestLeavesSum so we create a new function deepestLeave.12
3) now we go in depth, if we are at bottom, we return the value, we recursively visit both left and right nodes.16
def height(self, root):22
x = self.height(root.left) + 124
y = self.height(root.right) + 128
def deepestLeave(self, root, depth):33
if root.left is None and root.right is None:37
return self.deepestLeave(root.left, depth - 1) + self.deepestLeave(41
def deepestLeavesSum(self, root):46
return self.deepestLeave(root, self.height(root))