Sum of Nodes with Even-Valued Grandparent

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.

Constraints

  • The number of nodes in the tree is in the range [1, 10^4].
  • 1 <= Node.val <= 100
Saving… · 10 lines
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right

class Solution:
def sumOfNodesWithEvenValuedGrandparent(self, root: TreeNode) -> int:

Console

Click Run to execute sample tests.