Skip to content

Commit 9a66cae

Browse files
authored
feat: add solutions to lc problem: No.1315 (doocs#2461)
No.1315.Sum of Nodes with Even-Valued Grandparent
1 parent 04fe700 commit 9a66cae

File tree

7 files changed

+273
-205
lines changed

7 files changed

+273
-205
lines changed

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

Lines changed: 96 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@
3838

3939
## 解法
4040

41-
### 方法一
41+
### 方法一:DFS
42+
43+
我们设计一个函数 $dfs(root, x)$,表示以 $root$ 为根节点,并且 $root$ 的父节点的值为 $x$ 的子树中,满足条件的节点的值之和。那么答案就是 $dfs(root, 1)$。
44+
45+
函数 $dfs(root, x)$ 的执行过程如下:
46+
47+
- 如果 $root$ 为空,返回 $0$。
48+
- 否则,我们递归计算 $root$ 的左子树和右子树的答案,即 $dfs(root.left, root.val)$ 和 $dfs(root.right, root.val)$,累加到答案中。如果 $x$ 为偶数,此时我们判断 $root$ 的左孩子和右孩子是否存在,如果存在,我们将它们的值累加到答案中。
49+
- 最后返回答案。
50+
51+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点个数。
4252

4353
<!-- tabs:start -->
4454

@@ -51,22 +61,18 @@
5161
# self.right = right
5262
class Solution:
5363
def sumEvenGrandparent(self, root: TreeNode) -> int:
54-
self.res = 0
55-
56-
def dfs(g, p):
57-
if p is None:
58-
return
59-
if g.val % 2 == 0:
60-
if p.left:
61-
self.res += p.left.val
62-
if p.right:
63-
self.res += p.right.val
64-
dfs(p, p.left)
65-
dfs(p, p.right)
66-
67-
dfs(root, root.left)
68-
dfs(root, root.right)
69-
return self.res
64+
def dfs(root: TreeNode, x: int) -> int:
65+
if root is None:
66+
return 0
67+
ans = dfs(root.left, root.val) + dfs(root.right, root.val)
68+
if x % 2 == 0:
69+
if root.left:
70+
ans += root.left.val
71+
if root.right:
72+
ans += root.right.val
73+
return ans
74+
75+
return dfs(root, 1)
7076
```
7177

7278
```java
@@ -86,29 +92,24 @@ class Solution:
8692
* }
8793
*/
8894
class Solution {
89-
private int res;
90-
9195
public int sumEvenGrandparent(TreeNode root) {
92-
res = 0;
93-
dfs(root, root.left);
94-
dfs(root, root.right);
95-
return res;
96+
return dfs(root, 1);
9697
}
9798

98-
private void dfs(TreeNode g, TreeNode p) {
99-
if (p == null) {
100-
return;
99+
private int dfs(TreeNode root, int x) {
100+
if (root == null) {
101+
return 0;
101102
}
102-
if (g.val % 2 == 0) {
103-
if (p.left != null) {
104-
res += p.left.val;
103+
int ans = dfs(root.left, root.val) + dfs(root.right, root.val);
104+
if (x % 2 == 0) {
105+
if (root.left != null) {
106+
ans += root.left.val;
105107
}
106-
if (p.right != null) {
107-
res += p.right.val;
108+
if (root.right != null) {
109+
ans += root.right.val;
108110
}
109111
}
110-
dfs(p, p.left);
111-
dfs(p, p.right);
112+
return ans;
112113
}
113114
}
114115
```
@@ -127,23 +128,23 @@ class Solution {
127128
*/
128129
class Solution {
129130
public:
130-
int res;
131-
132131
int sumEvenGrandparent(TreeNode* root) {
133-
res = 0;
134-
dfs(root, root->left);
135-
dfs(root, root->right);
136-
return res;
137-
}
138-
139-
void dfs(TreeNode* g, TreeNode* p) {
140-
if (!p) return;
141-
if (g->val % 2 == 0) {
142-
if (p->left) res += p->left->val;
143-
if (p->right) res += p->right->val;
144-
}
145-
dfs(p, p->left);
146-
dfs(p, p->right);
132+
function<int(TreeNode*, int)> dfs = [&](TreeNode* root, int x) {
133+
if (!root) {
134+
return 0;
135+
}
136+
int ans = dfs(root->left, root->val) + dfs(root->right, root->val);
137+
if (x % 2 == 0) {
138+
if (root->left) {
139+
ans += root->left->val;
140+
}
141+
if (root->right) {
142+
ans += root->right->val;
143+
}
144+
}
145+
return ans;
146+
};
147+
return dfs(root, 1);
147148
}
148149
};
149150
```
@@ -157,30 +158,56 @@ public:
157158
* Right *TreeNode
158159
* }
159160
*/
160-
161-
var res int
162-
163161
func sumEvenGrandparent(root *TreeNode) int {
164-
res = 0
165-
dfs(root, root.Left)
166-
dfs(root, root.Right)
167-
return res
168-
}
169-
170-
func dfs(g, p *TreeNode) {
171-
if p == nil {
172-
return
173-
}
174-
if g.Val%2 == 0 {
175-
if p.Left != nil {
176-
res += p.Left.Val
162+
var dfs func(*TreeNode, int) int
163+
dfs = func(root *TreeNode, x int) int {
164+
if root == nil {
165+
return 0
177166
}
178-
if p.Right != nil {
179-
res += p.Right.Val
167+
ans := dfs(root.Left, root.Val) + dfs(root.Right, root.Val)
168+
if x%2 == 0 {
169+
if root.Left != nil {
170+
ans += root.Left.Val
171+
}
172+
if root.Right != nil {
173+
ans += root.Right.Val
174+
}
180175
}
176+
return ans
181177
}
182-
dfs(p, p.Left)
183-
dfs(p, p.Right)
178+
return dfs(root, 1)
179+
}
180+
```
181+
182+
```ts
183+
/**
184+
* Definition for a binary tree node.
185+
* class TreeNode {
186+
* val: number
187+
* left: TreeNode | null
188+
* right: TreeNode | null
189+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
190+
* this.val = (val===undefined ? 0 : val)
191+
* this.left = (left===undefined ? null : left)
192+
* this.right = (right===undefined ? null : right)
193+
* }
194+
* }
195+
*/
196+
197+
function sumEvenGrandparent(root: TreeNode | null): number {
198+
const dfs = (root: TreeNode | null, x: number): number => {
199+
if (!root) {
200+
return 0;
201+
}
202+
const { val, left, right } = root;
203+
let ans = dfs(left, val) + dfs(right, val);
204+
if (x % 2 === 0) {
205+
ans += left?.val ?? 0;
206+
ans += right?.val ?? 0;
207+
}
208+
return ans;
209+
};
210+
return dfs(root, 1);
184211
}
185212
```
186213

0 commit comments

Comments
 (0)