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 d7e07f2

Browse files
committed
Update README_EN.md
1 parent a00dd2b commit d7e07f2

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎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 -->

0 commit comments

Comments
(0)

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