Deepest Leaves Sum
To solve this coding challenge, the goal is to compute the sum of the values present in the deepest leaves of a binary tree. The binary tree is given by its root, and we need to traverse the tree to determine the depth of each node. Once we've identified the deepest level of the tree, we can sum the values of all nodes at that level.
Explanation
Let's break down the steps needed to solve the problem:- Define a Helper Function for Traversal : We'll use Depth-First Search (DFS) to traverse the tree. The DFS function will compute the level for each node, keeping track of the deepest level encountered so far.
- Track the Deepest Level and Values : During the traversal, we will keep updating a structure that holds the current deepest level and a list of values of the nodes at that deepest level.
- Update the Deepest Level Values : If we encounter a node at a deeper level than previously recorded, we update our deepest level and reset our list with the new node's value. If the node is at the same deepest level that we've recorded, simply add its value to the list.
- Sum the Deepest Level Values : After traversing the entire tree, we'll sum up the values stored in the list of deepest level nodes.
- Initialize a Structure for Deepest Level Information :
- Define the DFS Function :
- Traverse the Tree Starting from the Root :
- Compute and Return the Sum of Deepest Level Values :
- Start DFS Traversal :
- Invoke the DFS function starting from the root node with a depth of 0.
- Traverse Each Node :
- Increment the depth as we move down each level of the tree.
- When we encounter a non-null node, we recursively traverse its left and right children.
- Update Deepest Level Information :
- On visiting a node, we check if its depth is greater than the current recorded deepest level. If it is, update the deepest level and reset the list of values to include only this nodeβs value.
- If the depth is the same as the deepest level, append this node's value to the list of deepest values.
- Compute the Sum :
- Once the entire tree has been traversed, sum up the values in the list of deepest values and return the result.
Detailed Steps in Pseudocode
create a structure deepest_level_info with attributes:
- deepest_level initialized to 0
- deepest_values initialized to an empty list
function DFS(node, depth):
if node is None:
return
increment depth by 1
if node.left is not None:
call DFS(node.left, depth)
if node.right is not None:
call DFS(node.right, depth)
if depth is greater than deepest_level_info.deepest_level:
deepest_level_info.deepest_level = depth
deepest_level_info.deepest_values = [node.value]
else if depth is equal to deepest_level_info.deepest_level:
deepest_level_info.deepest_values.append(node.value)
call DFS on root with initial depth 0
return the sum of the elements in deepest_level_info.deepest_values
Pseudocode Implementation
Below is the complete pseudocode based on the above steps:
# Initialize structure for deepest level information
deepest_level_info = {
'deepest_level': 0,
'deepest_values': []
}
# Define the DFS function to traverse the binary tree
function DFS(node, depth):
# Base case: if the node is None, return
if node is None:
return
# Increment the current depth
depth = depth + 1
# Traverse the left subtree
if node.left is not None:
DFS(node.left, depth)
# Traverse the right subtree
if node.right is not None:
DFS(node.right, depth)
# Check and update deepest level information
if depth > deepest_level_info['deepest_level']:
# Deeper level found, update the deepest level and reset values
deepest_level_info['deepest_level'] = depth
deepest_level_info['deepest_values'] = [node.value]
else if depth == deepest_level_info['deepest_level']:
# Same level as current deepest, add value to deepest values
deepest_level_info['deepest_values'].append(node.value)
# Start DFS traversal from the root with initial depth 0
DFS(root, 0)
# Return the sum of the deepest leaves
return sum(deepest_level_info['deepest_values'])