File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,227 LeetCode solutions in JavaScript
1
+ # 1,228 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
996
996
1312|[ Minimum Insertion Steps to Make a String Palindrome] ( ./solutions/1312-minimum-insertion-steps-to-make-a-string-palindrome.js ) |Hard|
997
997
1313|[ Decompress Run-Length Encoded List] ( ./solutions/1313-decompress-run-length-encoded-list.js ) |Easy|
998
998
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|
999
1000
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|
1000
1001
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|
1001
1002
1319|[ Number of Operations to Make Network Connected] ( ./solutions/1319-number-of-operations-to-make-network-connected.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments