Problem Number: 1302 Difficulty: Medium Category: Tree, Depth-First Search, Breadth-First Search, Binary Tree LeetCode Link: https://leetcode.com/problems/deepest-leaves-sum/
Given the root of 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
Explanation: The deepest leaves are the nodes with values 7 and 8 respectively.
The sum of their values is 7 + 8 = 15.
Example 2:
Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output: 19
Constraints:
- The number of nodes in the tree is in the range
[1, 10^4]. 1 <= Node.val <= 100
I used a Depth-First Search (DFS) approach with depth tracking. The key insight is to traverse the tree and keep track of the maximum depth found, then sum all leaf nodes at that maximum depth.
Algorithm:
- Initialize maximum depth and sum variables
- Use DFS to traverse the tree recursively
- For each leaf node, check if it's at the deepest level
- If current depth is greater than maximum depth, update maximum depth and reset sum
- If current depth equals maximum depth, add leaf value to sum
- Recursively traverse left and right children
- Return the sum of deepest leaves
The solution uses DFS with depth tracking to find the sum of deepest leaves. See the implementation in the solution file.
Key Points:
- Uses recursive DFS to traverse the tree
- Tracks maximum depth found so far
- Resets sum when deeper level is found
- Accumulates sum for leaves at maximum depth
- Handles leaf nodes (nodes with no children)
- Returns sum of all deepest leaves
Time Complexity: O(n)
- DFS visits each node exactly once
- Each node operation is constant time
- Total: O(n)
Space Complexity: O(h)
- h is the height of the tree
- Recursion stack space
- In worst case (skewed tree): O(n)
- In best case (balanced tree): O(log n)
-
DFS Traversal: Using DFS allows us to explore the tree and find the deepest level.
-
Depth Tracking: We need to track the maximum depth to identify the deepest leaves.
-
Leaf Detection: A leaf node is identified when it has no left and right children.
-
Sum Accumulation: When we find leaves at the maximum depth, we accumulate their values.
-
Depth Reset: When we find a deeper level, we reset the sum and update the maximum depth.
-
Recursive Structure: The tree structure naturally lends itself to recursive traversal.
-
Wrong Traversal: Initially might use BFS when DFS is more appropriate for depth tracking.
-
Complex Logic: Overcomplicating the depth tracking with unnecessary data structures.
-
Wrong Leaf Detection: Not properly identifying leaf nodes.
-
Sum Logic: Not correctly handling the case when deeper levels are found.
- Maximum Depth of Binary Tree (Problem 104): Find maximum depth of binary tree
- Binary Tree Level Order Traversal (Problem 102): Level-by-level traversal
- Sum Root to Leaf Numbers (Problem 129): Sum of all root-to-leaf paths
- Path Sum (Problem 112): Check if path sum equals target
- Breadth-First Search: Use BFS with level tracking - O(n) time, O(w) space
- Two Pass DFS: First pass to find max depth, second pass to sum leaves - O(n) time
- Iterative DFS: Use stack for iterative DFS - O(n) time, O(h) space
- Wrong Traversal: Using BFS when DFS is more suitable for depth tracking.
- Complex Implementation: Overcomplicating the depth tracking logic.
- Wrong Leaf Detection: Not properly identifying leaf nodes.
- Sum Logic Errors: Not correctly handling depth updates and sum resets.
- Space Complexity: Not considering recursion stack space.
Note: This is a tree traversal problem that demonstrates efficient depth tracking with DFS.