Skip to content

Commit c3fed05

Browse files
author
Partho Biswas
committed
1315. Sum of Nodes with Even-Valued Grandparent
1 parent 843963c commit c3fed05

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
430430
|36| **[987. Vertical Order Traversal of a Binary Tree](https://tinyurl.com/y8vqh9hk)**| [Python](https://tinyurl.com/wu6rdaw/987_Vertical_Order_Traversal_of_a_Binary_Tree.py), [Swift](https://tinyurl.com/wuja3c4/987_Vertical_Order_Traversal_of_a_Binary_Tree.swift) | [Vid 1](https://tinyurl.com/y8byrezh), [Art 1](https://tinyurl.com/yajsqrpp), [Art 2](https://tinyurl.com/yadjj6sy) | Medium | |
431431
|37| **[958. Check Completeness of a Binary Tree](https://tinyurl.com/yd6dsk88)**| [Python](https://tinyurl.com/wu6rdaw/958_Check_Completeness_of_a_Binary_Tree.py), [Swift](https://tinyurl.com/wuja3c4/958_Check_Completeness_of_a_Binary_Tree.swift) | [Art 1](https://tinyurl.com/y729zfol) | Medium | |
432432
|38| **[865. Smallest Subtree with all the Deepest Nodes](https://tinyurl.com/y8cbxe4f)**| [Python](https://tinyurl.com/wu6rdaw/865_Smallest_Subtree_with_all_the_Deepest_Nodes.py), [Swift](https://tinyurl.com/wuja3c4/865_Smallest_Subtree_with_all_the_Deepest_Nodes.swift) | [Art 1](https://tinyurl.com/y9vq89jq), [Art 2](https://tinyurl.com/ydhfjr8t), [Art 3](https://tinyurl.com/y8z5p8u2), [Art 4](https://tinyurl.com/y8jm985p) | Medium | Vary ambigious question |
433+
|39| [1315. Sum of Nodes with Even-Valued Grandparent](https://tinyurl.com/y72q6hfn) | [Python](https://tinyurl.com/wu6rdaw/1315_Sum_of_Nodes_with_Even_Valued_Grandparent.py), [Swift](https://tinyurl.com/wuja3c4/1315_Sum_of_Nodes_with_Even_Valued_Grandparent.swift) | -- | Medium | -- |
433434

434435
</p>
435436
</details>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
# Top-Down
9+
class Solution(object):
10+
def sumEvenGrandparent(self, root):
11+
"""
12+
:type root: TreeNode
13+
:rtype: int
14+
"""
15+
self.nodeSumGlobal = 0
16+
self.sumEvenGrandparentHelper(root, None, None)
17+
return self.nodeSumGlobal
18+
19+
def sumEvenGrandparentHelper(self, root, parent, grandParent):
20+
if not root:
21+
return 0
22+
if grandParent and (grandParent.val % 2 == 0):
23+
self.nodeSumGlobal += root.val
24+
25+
self.sumEvenGrandparentHelper(root.left, root, parent)
26+
self.sumEvenGrandparentHelper(root.right, root, parent)
27+
28+
29+
# Bottom-Up
30+
class Solution(object):
31+
def sumEvenGrandparent(self, root):
32+
"""
33+
:type root: TreeNode
34+
:rtype: int
35+
"""
36+
nodeSum = self.sumEvenGrandparentHelper(root, None, None)
37+
return nodeSum
38+
39+
def sumEvenGrandparentHelper(self, root, parent, grandParent):
40+
if not root:
41+
return 0
42+
43+
left = self.sumEvenGrandparentHelper(root.left, root, parent)
44+
right = self.sumEvenGrandparentHelper(root.right, root, parent)
45+
46+
currentSum = left + right
47+
if grandParent and (grandParent.val % 2 == 0):
48+
currentSum += root.val
49+
return currentSum

0 commit comments

Comments
 (0)