Skip to content

Commit 90fb109

Browse files
authored
feat: add cpp solution to lc problem: No.0404 (doocs#2586)
close doocs#2599
1 parent fd7e6f7 commit 90fb109

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,37 @@ class Solution {
9393
}
9494
```
9595

96+
```cpp
97+
/**
98+
* Definition for a binary tree node.
99+
* struct TreeNode {
100+
* int val;
101+
* TreeNode *left;
102+
* TreeNode *right;
103+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
104+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
105+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
106+
* };
107+
*/
108+
class Solution {
109+
public:
110+
int sumOfLeftLeaves(TreeNode* root) {
111+
if (!root) {
112+
return 0;
113+
}
114+
int ans = sumOfLeftLeaves(root->right);
115+
if (root->left) {
116+
if (!root->left->left && !root->left->right) {
117+
ans += root->left->val;
118+
} else {
119+
ans += sumOfLeftLeaves(root->left);
120+
}
121+
}
122+
return ans;
123+
}
124+
};
125+
```
126+
96127
```go
97128
/**
98129
* Definition for a binary tree node.

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

+31
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,37 @@ class Solution {
8787
}
8888
```
8989

90+
```cpp
91+
/**
92+
* Definition for a binary tree node.
93+
* struct TreeNode {
94+
* int val;
95+
* TreeNode *left;
96+
* TreeNode *right;
97+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
98+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
99+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
100+
* };
101+
*/
102+
class Solution {
103+
public:
104+
int sumOfLeftLeaves(TreeNode* root) {
105+
if (!root) {
106+
return 0;
107+
}
108+
int ans = sumOfLeftLeaves(root->right);
109+
if (root->left) {
110+
if (!root->left->left && !root->left->right) {
111+
ans += root->left->val;
112+
} else {
113+
ans += sumOfLeftLeaves(root->left);
114+
}
115+
}
116+
return ans;
117+
}
118+
};
119+
```
120+
90121
```go
91122
/**
92123
* Definition for a binary tree node.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 = sumOfLeftLeaves(root->right);
19+
if (root->left) {
20+
if (!root->left->left && !root->left->right) {
21+
ans += root->left->val;
22+
} else {
23+
ans += sumOfLeftLeaves(root->left);
24+
}
25+
}
26+
return ans;
27+
}
28+
};

0 commit comments

Comments
 (0)