Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 989 Bytes

File metadata and controls

38 lines (30 loc) · 989 Bytes

1302. Deepest Leaves Sum

Given 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: 15

Constraints:

  • The number of nodes in the tree is between 1 and 10^4.
  • The value of nodes is between 1 and 100.

Solutions (Python)

1. Level Order Traversal

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def deepestLeavesSum(self, root: TreeNode) -> int:
        curr = [root]

        while True:
            next = [n.left for n in curr if n.left]
            next.extend(n.right for n in curr if n.right)

            if not next:
                return sum(n.val for n in curr)

            curr = next