Given the root of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandparent, return 0.
A grandparent of a node is the parent of its parent if it exists.
Example 1:
Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5] Output: 18 Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.
Example 2:
Input: root = [1] Output: 0
Constraints:
[1, 104].1 <= Node.val <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find the sum of the values of nodes that have a grandparent with an even value. The brute-force method involves checking every node in the tree to see if it meets the condition by exploring its ancestors.
Here's how the algorithm would work step-by-step:
def sum_of_even_grandparent_brute_force(root):
total_sum = 0
def traverse(node):
nonlocal total_sum
if not node:
return
# Check if the current node has a grandparent with an even value.
if node.parent and node.parent.parent:
grandparent = node.parent.parent
if grandparent.val % 2 == 0:
# Add the current node's value to the running total.
total_sum += node.val
# Recursively traverse the left and right subtrees.
traverse(node.left)
traverse(node.right)
# Begin the traversal from the root.
traverse(root)
return total_sumWe'll traverse the tree while keeping track of grandparent nodes. The key is to look two levels up from each node. If a grandparent exists and has an even value, we add the current node's value to a running total.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def sum_even_grandparent(root):
total_sum = 0
def dfs(node, parent, grandparent):
nonlocal total_sum
if not node:
return
# Check if grandparent exists and its value is even
if grandparent and grandparent.val % 2 == 0:
total_sum += node.val
# Traverse the left subtree
dfs(node.left, node, parent)
# Traverse the right subtree
dfs(node.right, node, parent)
# Initiate DFS traversal with None for parent and grandparent
dfs(root, None, None)
return total_sum| Case | How to Handle |
|---|---|
| Null root | Return 0 immediately as there are no nodes to process. |
| Single node tree | Return 0, as a single node cannot have a grandparent. |
| Two node tree (root and one child) | Return 0, as neither node can have a grandparent. |
| Grandparent with an even value has only one child | The algorithm should correctly check for the existence of both left and right grandchildren even if only one exists. |
| Grandparent with an even value has grandchildren with negative values. | The algorithm should correctly handle negative node values when calculating the sum. |
| Tree with a deep and highly unbalanced structure. | The recursive approach should handle unbalanced trees without stack overflow errors, or an iterative approach should be favored. |
| Large integer values in the nodes leading to integer overflow during summation. | Use a data type that can accommodate large sums, such as long. |
| Tree with many nodes where most grandparent values are even. | The solution should iterate efficiently through the tree without unnecessary computations. |