# 1302. Deepest Leaves Sum

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

Example:

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

# Solution

Approach 1: just do it -- any kind of tree traversals would work.

# Code (Python)

Approach 1:

    def deepestLeavesSum(self, root: TreeNode) -> int:
        # DFS
        if not root:
            return 0
        
        stack = [(root, 0)]
        total = 0
        max_depth = 0
        while stack:
            node, depth = stack.pop()
            for child in (node.left, node.right):
                if child:
                    stack.append((child, depth + 1))
            if node.left or node.right:
                continue
            if depth > max_depth:
                total = node.val
                max_depth = depth
            elif depth == max_depth:
                total += node.val

        return total

# Code (C++)

Approach 1:

Approach 2: