Skip to content

Commit b0ea7f4

Browse files
authored
Update README_EN.md
1 parent e6ef54e commit b0ea7f4

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

+44
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,48 @@ int sumOfLeftLeaves(struct TreeNode* root) {
215215
216216
<!-- tabs:end -->
217217
218+
### Solution 2
219+
220+
<!-- tabs:start -->
221+
222+
```cpp
223+
/**
224+
* Definition for a binary tree node.
225+
* struct TreeNode {
226+
* int val;
227+
* TreeNode *left;
228+
* TreeNode *right;
229+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
230+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
231+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
232+
* };
233+
*/
234+
class Solution {
235+
public:
236+
int sumOfLeftLeaves(TreeNode* root) {
237+
if (!root) {
238+
return 0;
239+
}
240+
int ans = 0;
241+
stack<TreeNode*> stk{{root}};
242+
while (!stk.empty()) {
243+
root = stk.top(), stk.pop();
244+
if (root->left) {
245+
if (!root->left->left && !root->left->right) {
246+
ans += root->left->val;
247+
} else {
248+
stk.push(root->left);
249+
}
250+
}
251+
if (root->right) {
252+
stk.push(root->right);
253+
}
254+
}
255+
return ans;
256+
}
257+
};
258+
```
259+
260+
<!-- tabs:end -->
261+
218262
<!-- end -->

0 commit comments

Comments
 (0)