1
class Solution {
2
int sum = 0;
3

4
public int sumEvenGrandparent(TreeNode root) {
5
dfs(root, null, null);
6
return sum;
7
}
8

9
void dfs(TreeNode current, TreeNode parent, TreeNode grandParent) {
10
if (current == null) return; // base case
11
if (grandParent != null && grandParent.val % 2 == 0) {
12
sum += current.val;
13
}
14

15
// cur->cur.left ||cur.right , parent=cur,grandPrarent=parent
16
dfs(current.left, current, parent);
17
dfs(current.right, current, parent);
18
}
19
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0