From 91de513ab7f4185650e722646f92f14d8eded4a8 Mon Sep 17 00:00:00 2001
From: Ali Nawaz <110383490+alipythondev@users.noreply.github.com>
Date: 2024年4月15日 10:29:59 +0500
Subject: [PATCH 1/4] Create Solution2.cpp
---
.../0404.Sum of Left Leaves/Solution2.cpp | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
create mode 100644 solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp
diff --git a/solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp b/solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp
new file mode 100644
index 0000000000000..076f33acb0c09
--- /dev/null
+++ b/solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp
@@ -0,0 +1,27 @@
+//Approach 2: Iterative
+//Time: O(n)
+//Space: O(h)
+class Solution {
+ public:
+ int sumOfLeftLeaves(TreeNode* root) {
+ if (root == nullptr)
+ return 0;
+
+ int ans = 0;
+ stack
stack{{root}};
+
+ while (!stack.empty()) {
+ root = stack.top(), stack.pop();
+ if (root->left) {
+ if (root->left->left == nullptr && root->left->right == nullptr)
+ ans += root->left->val;
+ else
+ stack.push(root->left);
+ }
+ if (root->right)
+ stack.push(root->right);
+ }
+
+ return ans;
+ }
+};
From 6cdc0f1744410e9b6a87de9fb7ff77cb31ff0db4 Mon Sep 17 00:00:00 2001
From: Libin YANG
Date: 2024年4月17日 16:47:46 +0800
Subject: [PATCH 2/4] Update Solution2.cpp
---
.../0404.Sum of Left Leaves/Solution2.cpp | 56 +++++++++++--------
1 file changed, 32 insertions(+), 24 deletions(-)
diff --git a/solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp b/solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp
index 076f33acb0c09..9254655caa629 100644
--- a/solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp
+++ b/solution/0400-0499/0404.Sum of Left Leaves/Solution2.cpp
@@ -1,27 +1,35 @@
-//Approach 2: Iterative
-//Time: O(n)
-//Space: O(h)
+/**
+ * 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 == nullptr)
- return 0;
-
- int ans = 0;
- stack stack{{root}};
-
- while (!stack.empty()) {
- root = stack.top(), stack.pop();
- if (root->left) {
- if (root->left->left == nullptr && root->left->right == nullptr)
- ans += root->left->val;
- else
- stack.push(root->left);
- }
- if (root->right)
- stack.push(root->right);
+public:
+ int sumOfLeftLeaves(TreeNode* root) {
+ if (!root) {
+ return 0;
+ }
+ int ans = 0;
+ stack
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;
}
-
- return ans;
- }
};
From a00dd2bce42e597dbc74601f4bf654e74a918b8a Mon Sep 17 00:00:00 2001
From: Libin YANG
Date: 2024年4月17日 16:49:15 +0800
Subject: [PATCH 3/4] Update README.md
---
.../0404.Sum of Left Leaves/README.md | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/solution/0400-0499/0404.Sum of Left Leaves/README.md b/solution/0400-0499/0404.Sum of Left Leaves/README.md
index 8d6f1117c81c7..3945af2de95b9 100644
--- a/solution/0400-0499/0404.Sum of Left Leaves/README.md
+++ b/solution/0400-0499/0404.Sum of Left Leaves/README.md
@@ -252,4 +252,48 @@ int sumOfLeftLeaves(struct TreeNode* root) {
+### 方法二
+
+
+
+```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 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;
+ }
+};
+```
+
+
+
From d7e07f250f100b1e55a3490357a94af4e8f1f3a0 Mon Sep 17 00:00:00 2001
From: Libin YANG
Date: 2024年4月17日 16:50:25 +0800
Subject: [PATCH 4/4] Update README_EN.md
---
.../0404.Sum of Left Leaves/README_EN.md | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/solution/0400-0499/0404.Sum of Left Leaves/README_EN.md b/solution/0400-0499/0404.Sum of Left Leaves/README_EN.md
index 64afc2b3524a9..bf318f2fa2136 100644
--- a/solution/0400-0499/0404.Sum of Left Leaves/README_EN.md
+++ b/solution/0400-0499/0404.Sum of Left Leaves/README_EN.md
@@ -246,4 +246,48 @@ int sumOfLeftLeaves(struct TreeNode* root) {
+### Solution 2
+
+
+
+```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 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;
+ }
+};
+```
+
+
+