@@ -99,6 +99,31 @@ class Solution {
99
99
}
100
100
```
101
101
102
+ ### ** Go**
103
+
104
+ ``` go
105
+ /* *
106
+ * Definition for a binary tree node.
107
+ * type TreeNode struct {
108
+ * Val int
109
+ * Left *TreeNode
110
+ * Right *TreeNode
111
+ * }
112
+ */
113
+ func sumOfLeftLeaves (root *TreeNode ) int {
114
+ if root == nil {
115
+ return 0
116
+ }
117
+ res := 0
118
+ if root.Left != nil && root.Left .Left == nil && root.Left .Right == nil {
119
+ res += root.Left .Val
120
+ }
121
+ res += sumOfLeftLeaves (root.Left )
122
+ res += sumOfLeftLeaves (root.Right )
123
+ return res
124
+ }
125
+ ```
126
+
102
127
### ** TypeScript**
103
128
104
129
``` ts
@@ -117,21 +142,17 @@ class Solution {
117
142
*/
118
143
119
144
const dfs = (root : TreeNode | null , isLeft : boolean ) => {
120
- let res = 0 ;
145
+ if (! root ) {
146
+ return 0 ;
147
+ }
121
148
const { val, left, right } = root ;
122
- if (left == null && right == null ) {
149
+ if (! left && ! right ) {
123
150
if (isLeft ) {
124
151
return val ;
125
152
}
126
- return res ;
153
+ return 0 ;
127
154
}
128
- if (left != null ) {
129
- res += dfs (left , true );
130
- }
131
- if (right != null ) {
132
- res += dfs (right , false );
133
- }
134
- return res ;
155
+ return dfs (left , true ) + dfs (right , false );
135
156
};
136
157
137
158
function sumOfLeftLeaves(root : TreeNode | null ): number {
@@ -164,23 +185,19 @@ use std::rc::Rc;
164
185
use std :: cell :: RefCell ;
165
186
impl Solution {
166
187
fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, is_left : bool ) -> i32 {
188
+ if root . is_none () {
189
+ return 0 ;
190
+ }
167
191
let node = root . as_ref (). unwrap (). borrow ();
168
192
let left = & node . left;
169
193
let right = & node . right;
170
- let mut res = 0 ;
171
194
if left . is_none () && right . is_none () {
172
195
if is_left {
173
196
return node . val;
174
197
}
175
- return res ;
176
- }
177
- if left . is_some () {
178
- res += Self :: dfs (left , true );
179
- }
180
- if right . is_some () {
181
- res += Self :: dfs (right , false );
198
+ return 0 ;
182
199
}
183
- res
200
+ Self :: dfs ( left , true ) + Self :: dfs ( right , false )
184
201
}
185
202
186
203
pub fn sum_of_left_leaves (root : Option <Rc <RefCell <TreeNode >>>) -> i32 {
@@ -189,30 +206,30 @@ impl Solution {
189
206
}
190
207
```
191
208
192
- ### ** Go**
193
-
194
- <!-- 这里可写当前语言的特殊实现逻辑 -->
209
+ ### ** C**
195
210
196
- ``` go
211
+ ``` c
197
212
/* *
198
213
* Definition for a binary tree node.
199
- * type TreeNode struct {
200
- * Val int
201
- * Left * TreeNode
202
- * Right * TreeNode
203
- * }
214
+ * struct TreeNode {
215
+ * int val;
216
+ * struct TreeNode *left;
217
+ * struct TreeNode *right;
218
+ * };
204
219
*/
205
- func sumOfLeftLeaves (root *TreeNode ) int {
206
- if root == nil {
207
- return 0
208
- }
209
- res := 0
210
- if root.Left != nil && root.Left .Left == nil && root.Left .Right == nil {
211
- res += root.Left .Val
212
- }
213
- res += sumOfLeftLeaves (root.Left )
214
- res += sumOfLeftLeaves (root.Right )
215
- return res
220
+
221
+ int dfs (struct TreeNode * root, int isLeft) {
222
+ if (!root) {
223
+ return 0;
224
+ }
225
+ if (!root->left && !root->right) {
226
+ return isLeft ? root->val : 0;
227
+ }
228
+ return dfs(root->left, 1) + dfs(root->right, 0);
229
+ }
230
+
231
+ int sumOfLeftLeaves(struct TreeNode * root) {
232
+ return dfs(root, 0);
216
233
}
217
234
```
218
235
0 commit comments