Skip to content

Commit 1e77cf3

Browse files
add 1315
1 parent e198b96 commit 1e77cf3

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ _If you like this project, please leave me a star._ ★
1717
|1329|[Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1329.java) | |Medium||
1818
|1323|[Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1323.java) | |Easy|Math|
1919
|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy||
20+
|1315|[Sum of Nodes with Even-Valued Grandparent](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1315.java) | |Medium|Tree, DFS|
2021
|1313|[Decompress Run-Length Encoded List](https://leetcode.com/problems/decompress-run-length-encoded-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1313.java) | |Easy|Array|
2122
|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium||
2223
|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy||
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 1315. Sum of Nodes with Even-Valued Grandparent
7+
*
8+
* Given a binary tree, return the sum of values of nodes with even-valued grandparent.
9+
* (A grandparent of a node is the parent of its parent, if it exists.)
10+
* If there are no nodes with an even-valued grandparent, return 0.
11+
*
12+
* Example 1:
13+
* 6
14+
* / \
15+
* 7 8
16+
* / \ / \
17+
* 2 7 1 3
18+
* / / \ \
19+
* 9 1 4 5
20+
*
21+
* Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
22+
* Output: 18
23+
* Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.
24+
*
25+
* Constraints:
26+
* The number of nodes in the tree is between 1 and 10^4.
27+
* The value of nodes is between 1 and 100.
28+
* */
29+
public class _1315 {
30+
public static class Solution1 {
31+
public int sumEvenGrandparent(TreeNode root) {
32+
if (root == null) {
33+
return 0;
34+
}
35+
return dfs(root, root.left, 0) + dfs(root, root.right, 0);
36+
}
37+
38+
private int dfs(TreeNode grandparent, TreeNode parent, int sum) {
39+
if (grandparent == null || parent == null) {
40+
return sum;
41+
}
42+
if (grandparent.val % 2 == 0 && parent.left != null) {
43+
sum += parent.left.val;
44+
}
45+
if (grandparent.val % 2 == 0 && parent.right != null) {
46+
sum += parent.right.val;
47+
}
48+
sum = dfs(parent, parent.left, sum);
49+
sum = dfs(parent, parent.right, sum);
50+
return sum;
51+
}
52+
53+
}
54+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1315;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _1315Test {
14+
private static _1315.Solution1 solution1;
15+
private static TreeNode root;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1315.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 7, 8, 2, 7, 1, 3, 9, null, 1, 4, null, null, null, 5));
25+
TreeUtils.printBinaryTree(root);
26+
assertEquals(18, solution1.sumEvenGrandparent(root));
27+
}
28+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy