Skip to content

Commit b902df8

Browse files
committed
feat: add solutions to lc problem: No.1315. Sum of Nodes with Even-Valued Grandparent
1 parent 50f8f38 commit b902df8

File tree

6 files changed

+415
-15
lines changed

6 files changed

+415
-15
lines changed

solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README.md

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,167 @@
3434
<li>每个节点的值在&nbsp;<code>1</code> 到&nbsp;<code>100</code> 之间。</li>
3535
</ul>
3636

37-
3837
## 解法
3938

4039
<!-- 这里可写通用的实现逻辑 -->
4140

41+
深度优先搜索。
42+
4243
<!-- tabs:start -->
4344

4445
### **Python3**
4546

4647
<!-- 这里可写当前语言的特殊实现逻辑 -->
4748

4849
```python
49-
50+
# Definition for a binary tree node.
51+
# class TreeNode:
52+
# def __init__(self, val=0, left=None, right=None):
53+
# self.val = val
54+
# self.left = left
55+
# self.right = right
56+
class Solution:
57+
def sumEvenGrandparent(self, root: TreeNode) -> int:
58+
self.res = 0
59+
60+
def dfs(g, p):
61+
if p is None:
62+
return
63+
if g.val % 2 == 0:
64+
if p.left:
65+
self.res += p.left.val
66+
if p.right:
67+
self.res += p.right.val
68+
dfs(p, p.left)
69+
dfs(p, p.right)
70+
71+
dfs(root, root.left)
72+
dfs(root, root.right)
73+
return self.res
5074
```
5175

5276
### **Java**
5377

5478
<!-- 这里可写当前语言的特殊实现逻辑 -->
5579

5680
```java
81+
/**
82+
* Definition for a binary tree node.
83+
* public class TreeNode {
84+
* int val;
85+
* TreeNode left;
86+
* TreeNode right;
87+
* TreeNode() {}
88+
* TreeNode(int val) { this.val = val; }
89+
* TreeNode(int val, TreeNode left, TreeNode right) {
90+
* this.val = val;
91+
* this.left = left;
92+
* this.right = right;
93+
* }
94+
* }
95+
*/
96+
class Solution {
97+
private int res;
98+
99+
public int sumEvenGrandparent(TreeNode root) {
100+
res = 0;
101+
dfs(root, root.left);
102+
dfs(root, root.right);
103+
return res;
104+
}
105+
106+
private void dfs(TreeNode g, TreeNode p) {
107+
if (p == null) {
108+
return;
109+
}
110+
if (g.val % 2 == 0) {
111+
if (p.left != null) {
112+
res += p.left.val;
113+
}
114+
if (p.right != null) {
115+
res += p.right.val;
116+
}
117+
}
118+
dfs(p, p.left);
119+
dfs(p, p.right);
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
/**
128+
* Definition for a binary tree node.
129+
* struct TreeNode {
130+
* int val;
131+
* TreeNode *left;
132+
* TreeNode *right;
133+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
134+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
135+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
136+
* };
137+
*/
138+
class Solution {
139+
public:
140+
int res;
141+
142+
int sumEvenGrandparent(TreeNode* root) {
143+
res = 0;
144+
dfs(root, root->left);
145+
dfs(root, root->right);
146+
return res;
147+
}
148+
149+
void dfs(TreeNode* g, TreeNode* p) {
150+
if (!p) return;
151+
if (g->val % 2 == 0)
152+
{
153+
if (p->left) res += p->left->val;
154+
if (p->right) res += p->right->val;
155+
}
156+
dfs(p, p->left);
157+
dfs(p, p->right);
158+
}
159+
};
160+
```
57161
162+
### **Go**
163+
164+
```go
165+
/**
166+
* Definition for a binary tree node.
167+
* type TreeNode struct {
168+
* Val int
169+
* Left *TreeNode
170+
* Right *TreeNode
171+
* }
172+
*/
173+
174+
var res int
175+
176+
func sumEvenGrandparent(root *TreeNode) int {
177+
res = 0
178+
dfs(root, root.Left)
179+
dfs(root, root.Right)
180+
return res
181+
}
182+
183+
func dfs(g, p *TreeNode) {
184+
if p == nil {
185+
return
186+
}
187+
if g.Val%2 == 0 {
188+
if p.Left != nil {
189+
res += p.Left.Val
190+
}
191+
if p.Right != nil {
192+
res += p.Right.Val
193+
}
194+
}
195+
dfs(p, p.Left)
196+
dfs(p, p.Right)
197+
}
58198
```
59199

60200
### **...**

solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README_EN.md

Lines changed: 142 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
66

77
<p>Given a binary tree, return the sum of values of nodes with even-valued grandparent.&nbsp; (A <em>grandparent</em> of a node is the parent of its parent, if it exists.)</p>
88

9-
10-
119
<p>If there are no nodes with an even-valued grandparent, return&nbsp;<code>0</code>.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

1713
<p><strong>Example 1:</strong></p>
1814

19-
20-
2115
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1315.Sum%20of%20Nodes%20with%20Even-Valued%20Grandparent/images/1473_ex1.png" style="width: 350px; height: 214px;" /></strong></p>
2216

23-
24-
2517
<pre>
2618

2719
<strong>Input:</strong> root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
@@ -32,33 +24,170 @@
3224

3325
</pre>
3426

35-
36-
3727
<p>&nbsp;</p>
3828

3929
<p><strong>Constraints:</strong></p>
4030

41-
42-
4331
<ul>
4432
<li>The number of nodes in the tree is between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>10^4</code>.</li>
4533
<li>The value of nodes is between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>100</code>.</li>
4634
</ul>
4735

4836
## Solutions
4937

38+
DFS.
39+
5040
<!-- tabs:start -->
5141

5242
### **Python3**
5343

5444
```python
55-
45+
# Definition for a binary tree node.
46+
# class TreeNode:
47+
# def __init__(self, val=0, left=None, right=None):
48+
# self.val = val
49+
# self.left = left
50+
# self.right = right
51+
class Solution:
52+
def sumEvenGrandparent(self, root: TreeNode) -> int:
53+
self.res = 0
54+
55+
def dfs(g, p):
56+
if p is None:
57+
return
58+
if g.val % 2 == 0:
59+
if p.left:
60+
self.res += p.left.val
61+
if p.right:
62+
self.res += p.right.val
63+
dfs(p, p.left)
64+
dfs(p, p.right)
65+
66+
dfs(root, root.left)
67+
dfs(root, root.right)
68+
return self.res
5669
```
5770

5871
### **Java**
5972

6073
```java
74+
/**
75+
* Definition for a binary tree node.
76+
* public class TreeNode {
77+
* int val;
78+
* TreeNode left;
79+
* TreeNode right;
80+
* TreeNode() {}
81+
* TreeNode(int val) { this.val = val; }
82+
* TreeNode(int val, TreeNode left, TreeNode right) {
83+
* this.val = val;
84+
* this.left = left;
85+
* this.right = right;
86+
* }
87+
* }
88+
*/
89+
class Solution {
90+
private int res;
91+
92+
public int sumEvenGrandparent(TreeNode root) {
93+
res = 0;
94+
dfs(root, root.left);
95+
dfs(root, root.right);
96+
return res;
97+
}
98+
99+
private void dfs(TreeNode g, TreeNode p) {
100+
if (p == null) {
101+
return;
102+
}
103+
if (g.val % 2 == 0) {
104+
if (p.left != null) {
105+
res += p.left.val;
106+
}
107+
if (p.right != null) {
108+
res += p.right.val;
109+
}
110+
}
111+
dfs(p, p.left);
112+
dfs(p, p.right);
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
/**
121+
* Definition for a binary tree node.
122+
* struct TreeNode {
123+
* int val;
124+
* TreeNode *left;
125+
* TreeNode *right;
126+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
127+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
128+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
129+
* };
130+
*/
131+
class Solution {
132+
public:
133+
int res;
134+
135+
int sumEvenGrandparent(TreeNode* root) {
136+
res = 0;
137+
dfs(root, root->left);
138+
dfs(root, root->right);
139+
return res;
140+
}
141+
142+
void dfs(TreeNode* g, TreeNode* p) {
143+
if (!p) return;
144+
if (g->val % 2 == 0)
145+
{
146+
if (p->left) res += p->left->val;
147+
if (p->right) res += p->right->val;
148+
}
149+
dfs(p, p->left);
150+
dfs(p, p->right);
151+
}
152+
};
153+
```
61154
155+
### **Go**
156+
157+
```go
158+
/**
159+
* Definition for a binary tree node.
160+
* type TreeNode struct {
161+
* Val int
162+
* Left *TreeNode
163+
* Right *TreeNode
164+
* }
165+
*/
166+
167+
var res int
168+
169+
func sumEvenGrandparent(root *TreeNode) int {
170+
res = 0
171+
dfs(root, root.Left)
172+
dfs(root, root.Right)
173+
return res
174+
}
175+
176+
func dfs(g, p *TreeNode) {
177+
if p == nil {
178+
return
179+
}
180+
if g.Val%2 == 0 {
181+
if p.Left != nil {
182+
res += p.Left.Val
183+
}
184+
if p.Right != nil {
185+
res += p.Right.Val
186+
}
187+
}
188+
dfs(p, p.Left)
189+
dfs(p, p.Right)
190+
}
62191
```
63192

64193
### **...**

0 commit comments

Comments
 (0)