Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb7835f

Browse files
feat: add cpp solution to lc problem: No.0404 (#2587)
close #2600
1 parent 90fb109 commit fb7835f

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

‎solution/0400-0499/0404.Sum of Left Leaves/README.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,48 @@ int sumOfLeftLeaves(struct TreeNode* root) {
252252
253253
<!-- tabs:end -->
254254
255+
### 方法二
256+
257+
<!-- tabs:start -->
258+
259+
```cpp
260+
/**
261+
* Definition for a binary tree node.
262+
* struct TreeNode {
263+
* int val;
264+
* TreeNode *left;
265+
* TreeNode *right;
266+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
267+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
268+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
269+
* };
270+
*/
271+
class Solution {
272+
public:
273+
int sumOfLeftLeaves(TreeNode* root) {
274+
if (!root) {
275+
return 0;
276+
}
277+
int ans = 0;
278+
stack<TreeNode*> stk{{root}};
279+
while (!stk.empty()) {
280+
root = stk.top(), stk.pop();
281+
if (root->left) {
282+
if (!root->left->left && !root->left->right) {
283+
ans += root->left->val;
284+
} else {
285+
stk.push(root->left);
286+
}
287+
}
288+
if (root->right) {
289+
stk.push(root->right);
290+
}
291+
}
292+
return ans;
293+
}
294+
};
295+
```
296+
297+
<!-- tabs:end -->
298+
255299
<!-- end -->

‎solution/0400-0499/0404.Sum of Left Leaves/README_EN.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,48 @@ int sumOfLeftLeaves(struct TreeNode* root) {
246246
247247
<!-- tabs:end -->
248248
249+
### Solution 2
250+
251+
<!-- tabs:start -->
252+
253+
```cpp
254+
/**
255+
* Definition for a binary tree node.
256+
* struct TreeNode {
257+
* int val;
258+
* TreeNode *left;
259+
* TreeNode *right;
260+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
261+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
262+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
263+
* };
264+
*/
265+
class Solution {
266+
public:
267+
int sumOfLeftLeaves(TreeNode* root) {
268+
if (!root) {
269+
return 0;
270+
}
271+
int ans = 0;
272+
stack<TreeNode*> stk{{root}};
273+
while (!stk.empty()) {
274+
root = stk.top(), stk.pop();
275+
if (root->left) {
276+
if (!root->left->left && !root->left->right) {
277+
ans += root->left->val;
278+
} else {
279+
stk.push(root->left);
280+
}
281+
}
282+
if (root->right) {
283+
stk.push(root->right);
284+
}
285+
}
286+
return ans;
287+
}
288+
};
289+
```
290+
291+
<!-- tabs:end -->
292+
249293
<!-- end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int sumOfLeftLeaves(TreeNode* root) {
15+
if (!root) {
16+
return 0;
17+
}
18+
int ans = 0;
19+
stack<TreeNode*> stk{{root}};
20+
while (!stk.empty()) {
21+
root = stk.top(), stk.pop();
22+
if (root->left) {
23+
if (!root->left->left && !root->left->right) {
24+
ans += root->left->val;
25+
} else {
26+
stk.push(root->left);
27+
}
28+
}
29+
if (root->right) {
30+
stk.push(root->right);
31+
}
32+
}
33+
return ans;
34+
}
35+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /