#1315
Sum of Nodes with Even-Valued Grandparent
pupil · 385 · lc medium +24 · verified · 85.9% accepted · 2,828 likes · top 97%
Description
Given the root of a binary tree, sum the values of all nodes whose grandparent (the parent of their parent) has an even value. Return 0 if no such grandparent exists anywhere in the tree.
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
Code
1
2
3
4
5
6
7
8
9