Sum of Nodes with Even-Valued Grandparent - Problem

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. Think of it as traversing three generations in the tree: grandparent โ†’ parent โ†’ grandchild.

Example: If a node has value 6 (even), then all of its grandchildren's values should be included in our sum. We need to look two levels down from each even-valued node.

This problem tests your ability to track relationships across multiple levels of a tree structure while traversing efficiently.

Input & Output

example_1.py โ€” Standard Binary Tree
$ Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
โ€บ Output: 18
๐Ÿ’ก Note: Node 6 is the only even-valued grandparent. Its grandchildren are nodes 2, 7, 1, and 3. The sum is 2 + 7 + 1 + 3 = 13. Wait, let me recalculate: Looking at grandparent 6, its children are 7 and 8, so grandchildren are 2, 7 (left subtree) and 1, 3 (right subtree). Actually checking nodes with even grandparent 6: we get 2+7+1+3=13. But the expected is 18, so there might be another even grandparent like 8. Node 8's grandchildren would be: none from left child 1, and node 5 from right child 3. So total: 2+7+1+3+5=18.
example_2.py โ€” Single Node
$ Input: root = [1]
โ€บ Output: 0
๐Ÿ’ก Note: There are no grandparents in a tree with only one node, so the sum is 0.
example_3.py โ€” No Even Grandparents
$ Input: root = [1,3,5,7,9]
โ€บ Output: 0
๐Ÿ’ก Note: All values are odd, so there are no even-valued grandparents. The sum is 0.

Visualization

Tap to expand
Family Tree Inheritance System$6Rich Grandpa (even account)$7$8Rich Grandpa too!$2$7$1$3$5๐Ÿ’ฐ Grandparent with even account ($6, $8)๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Parent generation๐ŸŽ Grandchildren receiving inheritanceTotal Inheritance: $2+$7+$1+$3+$5 = $18
Understanding the Visualization
1
Brute Force: Ask Everyone
Go to each person and ask: 'Do you have a grandparent with an even bank account?' This requires everyone to remember their family tree.
2
Smart Approach: Pass Information Down
Start from the eldest generation and pass down information: 'Your grandparent has account #6 (even), so you get inheritance!'
3
Optimal Traversal
In one family reunion, efficiently pass grandparent info to each generation. No need to ask backward, just pass forward.
4
Calculate Total
Sum up all the inheritance money distributed to grandchildren of wealthy (even-numbered) grandparents.
Key Takeaway
๐ŸŽฏ Key Insight: Instead of each grandchild searching backward for their grandparent, we pass the grandparent's information forward during our family tree traversal. This transforms an O(nยฒ) backward-lookup problem into an elegant O(n) forward-passing solution!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Visit each node exactly once during DFS traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height, O(log n) for balanced tree, O(n) worst case

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • 1 โ‰ค Node.val โ‰ค 100
  • Tree nodes are connected in a valid binary tree structure
Asked in
Facebook 15 Amazon 12 Google 8 Microsoft 6
54.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen