Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Solution: Sum of Nodes with Even-Valued Grandparent
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given the root of a binary tree, return the total sum of the values of all nodes that have a grandparent with an even number. If no such nodes exist, return 0.

A grandparent of a node is defined as the parent of its parent, if both exist.

Examples

Example 1:

  • Input: root = [4, 2, 6, 3, 5, null, 8]
  • Expected Output: 16
  • Justification: Node values 3, 5, and 8 have 4 as their grandparent. Since 4 is even, their sum is 3 + 5 + 8 = 16.

Example 2:

  • Input: root = `[2, 3, 8, null, 5, 7, null, 2, 3, 4, 5]

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible