38
38
39
39
## 解法
40
40
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$ 为节点个数。
42
52
43
53
<!-- tabs:start -->
44
54
51
61
# self.right = right
52
62
class Solution :
53
63
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 )
70
76
```
71
77
72
78
``` java
@@ -86,29 +92,24 @@ class Solution:
86
92
* }
87
93
*/
88
94
class Solution {
89
- private int res;
90
-
91
95
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 );
96
97
}
97
98
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 ;
101
102
}
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;
105
107
}
106
- if (p . right != null ) {
107
- res += p . right. val;
108
+ if (root . right != null ) {
109
+ ans += root . right. val;
108
110
}
109
111
}
110
- dfs(p, p. left);
111
- dfs(p, p. right);
112
+ return ans;
112
113
}
113
114
}
114
115
```
@@ -127,23 +128,23 @@ class Solution {
127
128
*/
128
129
class Solution {
129
130
public:
130
- int res;
131
-
132
131
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);
147
148
}
148
149
};
149
150
```
@@ -157,30 +158,56 @@ public:
157
158
* Right *TreeNode
158
159
* }
159
160
*/
160
-
161
- var res int
162
-
163
161
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
177
166
}
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
+ }
180
175
}
176
+ return ans
181
177
}
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 );
184
211
}
185
212
```
186
213
0 commit comments