Description#
Given the root
of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandparent, return 0
.
A grandparent of a node is the parent of its parent if it exists.
Example 1:
Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output: 18
Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.
Example 2:
Input: root = [1]
Output: 0
Constraints:
- The number of nodes in the tree is in the range
[1, 104]
. 1 <= Node.val <= 100
Solutions#
Solution 1#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumEvenGrandparent(self, root: TreeNode) -> int:
self.res = 0
def dfs(g, p):
if p is None:
return
if g.val % 2 == 0:
if p.left:
self.res += p.left.val
if p.right:
self.res += p.right.val
dfs(p, p.left)
dfs(p, p.right)
dfs(root, root.left)
dfs(root, root.right)
return self.res
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int res;
public int sumEvenGrandparent(TreeNode root) {
res = 0;
dfs(root, root.left);
dfs(root, root.right);
return res;
}
private void dfs(TreeNode g, TreeNode p) {
if (p == null) {
return;
}
if (g.val % 2 == 0) {
if (p.left != null) {
res += p.left.val;
}
if (p.right != null) {
res += p.right.val;
}
}
dfs(p, p.left);
dfs(p, p.right);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int res;
int sumEvenGrandparent(TreeNode* root) {
res = 0;
dfs(root, root->left);
dfs(root, root->right);
return res;
}
void dfs(TreeNode* g, TreeNode* p) {
if (!p) return;
if (g->val % 2 == 0) {
if (p->left) res += p->left->val;
if (p->right) res += p->right->val;
}
dfs(p, p->left);
dfs(p, p->right);
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| /**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var res int
func sumEvenGrandparent(root *TreeNode) int {
res = 0
dfs(root, root.Left)
dfs(root, root.Right)
return res
}
func dfs(g, p *TreeNode) {
if p == nil {
return
}
if g.Val%2 == 0 {
if p.Left != nil {
res += p.Left.Val
}
if p.Right != nil {
res += p.Right.Val
}
}
dfs(p, p.Left)
dfs(p, p.Right)
}
|