Skip to content

Commit 1a5ab1a

Browse files
committed
Add solution #1315
1 parent 45f5020 commit 1a5ab1a

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,227 LeetCode solutions in JavaScript
1+
# 1,228 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -996,6 +996,7 @@
996996
1312|[Minimum Insertion Steps to Make a String Palindrome](./solutions/1312-minimum-insertion-steps-to-make-a-string-palindrome.js)|Hard|
997997
1313|[Decompress Run-Length Encoded List](./solutions/1313-decompress-run-length-encoded-list.js)|Easy|
998998
1314|[Matrix Block Sum](./solutions/1314-matrix-block-sum.js)|Medium|
999+
1315|[Sum of Nodes with Even-Valued Grandparent](./solutions/1315-sum-of-nodes-with-even-valued-grandparent.js)|Medium|
9991000
1317|[Convert Integer to the Sum of Two No-Zero Integers](./solutions/1317-convert-integer-to-the-sum-of-two-no-zero-integers.js)|Easy|
10001001
1318|[Minimum Flips to Make a OR b Equal to c](./solutions/1318-minimum-flips-to-make-a-or-b-equal-to-c.js)|Medium|
10011002
1319|[Number of Operations to Make Network Connected](./solutions/1319-number-of-operations-to-make-network-connected.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1315. Sum of Nodes with Even-Valued Grandparent
3+
* https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/
4+
* Difficulty: Medium
5+
*
6+
* Given the root of a binary tree, return the sum of values of nodes with an even-valued
7+
* grandparent. If there are no nodes with an even-valued grandparent, return 0.
8+
*
9+
* A grandparent of a node is the parent of its parent if it exists.
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* function TreeNode(val, left, right) {
15+
* this.val = (val===undefined ? 0 : val)
16+
* this.left = (left===undefined ? null : left)
17+
* this.right = (right===undefined ? null : right)
18+
* }
19+
*/
20+
/**
21+
* @param {TreeNode} root
22+
* @return {number}
23+
*/
24+
var sumEvenGrandparent = function(root) {
25+
let result = 0;
26+
exploreTree(root, null, null);
27+
return result;
28+
29+
function exploreTree(current, parent, grandparent) {
30+
if (!current) return;
31+
32+
if (grandparent?.val % 2 === 0) {
33+
result += current.val;
34+
}
35+
36+
exploreTree(current.left, current, parent);
37+
exploreTree(current.right, current, parent);
38+
}
39+
};

0 commit comments

Comments
 (0)