Leetcode 1302: Deepest Leaves Sum

grid47
grid47
Exploring patterns and algorithms
Jun 29, 2024 6 min read

Given the root of a binary tree, return the sum of values of its deepest leaves. The deepest leaves are the nodes found at the lowest level of the tree.
Problem
Approach
Steps
Complexity
Input: The input consists of a binary tree where each node is represented by a value.
Example: Input: root = [10, 5, 15, 3, 7, null, 20, null, null, 18]
Constraints:
• The number of nodes in the tree is in the range [1, 10^4].
• 1 <= Node.val <= 100
Output: Return an integer representing the sum of the values of the deepest leaves in the binary tree.
Example: Output: 38
Constraints:
• The sum of the values of the deepest leaves.
Goal: To calculate the sum of the values of the deepest leaves of the binary tree.
Steps:
• Perform a level-order traversal (breadth-first search) of the binary tree to find the deepest level.
• At each level, accumulate the values of all nodes. The sum of the last level's nodes will be the sum of the deepest leaves.
Goal: The solution must be optimized to handle binary trees with up to 10^4 nodes efficiently.
Steps:
• 1 <= Number of nodes <= 10^4
• Node values range from 1 to 100.
Assumptions:
• The input tree is valid and non-empty.
• The number of nodes in the tree will always be within the specified range.
Input: Input: root = [10, 5, 15, 3, 7, null, 20, null, null, 18]
Explanation: The deepest leaves are 18 and 20. Their sum is 38.

Input: Input: root = [12, 8, 20, 4, 10, 16, 25]
Explanation: The deepest leaves are 16 and 25. Their sum is 41.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus