Sum of Nodes with Even-Valued Grandparent (Leetcode 1315)
Problem Link: https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/
class Solution:
def sumEvenGrandparent(self, root: TreeNode) -> int:
if(root is None):
return 0
lans=self.sumEvenGrandparent(root.left)
rans=self.sumEvenGrandparent(root.right)
ans=0
if(root.val%2==0):
if(root.left is not None):
if(root.left.left is not None):
ans+=root.left.left.val
if(root.left.right is not None):
ans+=root.left.right.val
if(root.right is not None):
if(root.right.left is not None):
ans+=root.right.left.val
if(root.right.right is not None):
ans+=root.right.right.val
t
return lans+rans+ans
Last updated