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

feat: add cpp solution to lc problem: No.0404 #2587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 4 commits into doocs:main from AlleyNawaz:patch-2
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions solution/0400-0499/0404.Sum of Left Leaves/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,48 @@ int sumOfLeftLeaves(struct TreeNode* root) {

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if (!root) {
return 0;
}
int ans = 0;
stack<TreeNode*> stk{{root}};
while (!stk.empty()) {
root = stk.top(), stk.pop();
if (root->left) {
if (!root->left->left && !root->left->right) {
ans += root->left->val;
} else {
stk.push(root->left);
}
}
if (root->right) {
stk.push(root->right);
}
}
return ans;
}
};
```

<!-- tabs:end -->

<!-- end -->
44 changes: 44 additions & 0 deletions solution/0400-0499/0404.Sum of Left Leaves/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,48 @@ int sumOfLeftLeaves(struct TreeNode* root) {

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if (!root) {
return 0;
}
int ans = 0;
stack<TreeNode*> stk{{root}};
while (!stk.empty()) {
root = stk.top(), stk.pop();
if (root->left) {
if (!root->left->left && !root->left->right) {
ans += root->left->val;
} else {
stk.push(root->left);
}
}
if (root->right) {
stk.push(root->right);
}
}
return ans;
}
};
```

<!-- tabs:end -->

<!-- end -->
35 changes: 35 additions & 0 deletions solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if (!root) {
return 0;
}
int ans = 0;
stack<TreeNode*> stk{{root}};
while (!stk.empty()) {
root = stk.top(), stk.pop();
if (root->left) {
if (!root->left->left && !root->left->right) {
ans += root->left->val;
} else {
stk.push(root->left);
}
}
if (root->right) {
stk.push(root->right);
}
}
return ans;
}
};

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