Skip to content

Commit 043b54a

Browse files
committed
feat: add solutions to lc problem: No.0404
No.0404.Sum of Left Leaves
1 parent a32a03a commit 043b54a

File tree

11 files changed

+602
-139
lines changed

11 files changed

+602
-139
lines changed

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

+212-47
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,36 @@
4242

4343
## 解法
4444

45-
### 方法一
45+
### 方法一:递归
46+
47+
我们首先判断 `root` 是否为空,如果为空则返回 $0$。
48+
49+
否则,我们递归调用 `sumOfLeftLeaves` 函数计算 `root` 的右子树中所有左叶子之和,并将结果赋给答案变量 $ans$。然后我们判断 `root` 的左子节点是否存在,如果存在,我们判断其是否为叶子节点,如果是叶子节点,则将其值加到答案变量 $ans$ 中,否则我们递归调用 `sumOfLeftLeaves` 函数计算 `root` 的左子树中所有左叶子之和,并将结果加到答案变量 $ans$ 中。
50+
51+
最后返回答案即可。
52+
53+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。
4654

4755
<!-- tabs:start -->
4856

4957
```python
5058
# Definition for a binary tree node.
5159
# class TreeNode:
52-
# def __init__(self, x):
53-
# self.val = x
54-
# self.left = None
55-
# self.right = None
56-
57-
60+
# def __init__(self, val=0, left=None, right=None):
61+
# self.val = val
62+
# self.left = left
63+
# self.right = right
5864
class Solution:
59-
def sumOfLeftLeaves(self, root: TreeNode) -> int:
65+
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
6066
if root is None:
6167
return 0
62-
res = 0
63-
if root.left and root.left.left is None and root.left.right is None:
64-
res += root.left.val
65-
res += self.sumOfLeftLeaves(root.left)
66-
res += self.sumOfLeftLeaves(root.right)
67-
return res
68+
ans = self.sumOfLeftLeaves(root.right)
69+
if root.left:
70+
if root.left.left == root.left.right:
71+
ans += root.left.val
72+
else:
73+
ans += self.sumOfLeftLeaves(root.left)
74+
return ans
6875
```
6976

7077
```java
@@ -74,21 +81,29 @@ class Solution:
7481
* int val;
7582
* TreeNode left;
7683
* TreeNode right;
77-
* TreeNode(int x) { val = x; }
84+
* TreeNode() {}
85+
* TreeNode(int val) { this.val = val; }
86+
* TreeNode(int val, TreeNode left, TreeNode right) {
87+
* this.val = val;
88+
* this.left = left;
89+
* this.right = right;
90+
* }
7891
* }
7992
*/
8093
class Solution {
8194
public int sumOfLeftLeaves(TreeNode root) {
8295
if (root == null) {
8396
return 0;
8497
}
85-
int res = 0;
86-
if (root.left != null && root.left.left == null && root.left.right == null) {
87-
res += root.left.val;
98+
int ans = sumOfLeftLeaves(root.right);
99+
if (root.left != null) {
100+
if (root.left.left == root.left.right) {
101+
ans += root.left.val;
102+
} else {
103+
ans += sumOfLeftLeaves(root.left);
104+
}
88105
}
89-
res += sumOfLeftLeaves(root.left);
90-
res += sumOfLeftLeaves(root.right);
91-
return res;
106+
return ans;
92107
}
93108
}
94109
```
@@ -137,13 +152,15 @@ func sumOfLeftLeaves(root *TreeNode) int {
137152
if root == nil {
138153
return 0
139154
}
140-
res := 0
141-
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
142-
res += root.Left.Val
155+
ans := sumOfLeftLeaves(root.Right)
156+
if root.Left != nil {
157+
if root.Left.Left == root.Left.Right {
158+
ans += root.Left.Val
159+
} else {
160+
ans += sumOfLeftLeaves(root.Left)
161+
}
143162
}
144-
res += sumOfLeftLeaves(root.Left)
145-
res += sumOfLeftLeaves(root.Right)
146-
return res
163+
return ans
147164
}
148165
```
149166

@@ -162,22 +179,19 @@ func sumOfLeftLeaves(root *TreeNode) int {
162179
* }
163180
*/
164181

165-
const dfs = (root: TreeNode | null, isLeft: boolean) => {
182+
function sumOfLeftLeaves(root: TreeNode | null): number {
166183
if (!root) {
167184
return 0;
168185
}
169-
const { val, left, right } = root;
170-
if (!left && !right) {
171-
if (isLeft) {
172-
return val;
186+
let ans = sumOfLeftLeaves(root.right);
187+
if (root.left) {
188+
if (root.left.left === root.left.right) {
189+
ans += root.left.val;
190+
} else {
191+
ans += sumOfLeftLeaves(root.left);
173192
}
174-
return 0;
175193
}
176-
return dfs(left, true) + dfs(right, false);
177-
};
178-
179-
function sumOfLeftLeaves(root: TreeNode | null): number {
180-
return dfs(root, false);
194+
return ans;
181195
}
182196
```
183197

@@ -235,27 +249,108 @@ impl Solution {
235249
* };
236250
*/
237251

238-
int dfs(struct TreeNode* root, int isLeft) {
252+
253+
int sumOfLeftLeaves(struct TreeNode* root){
239254
if (!root) {
240255
return 0;
241256
}
242-
if (!root->left && !root->right) {
243-
return isLeft ? root->val : 0;
257+
int ans = sumOfLeftLeaves(root->right);
258+
if (root->left) {
259+
if (!root->left->left && !root->left->right) {
260+
ans += root->left->val;
261+
} else {
262+
ans += sumOfLeftLeaves(root->left);
263+
}
244264
}
245-
return dfs(root->left, 1) + dfs(root->right, 0);
246-
}
247-
248-
int sumOfLeftLeaves(struct TreeNode* root) {
249-
return dfs(root, 0);
265+
return ans;
250266
}
251267
```
252268
253269
<!-- tabs:end -->
254270
255-
### 方法二
271+
### 方法二:栈
272+
273+
我们也可以将方法一的递归改为迭代,使用栈来模拟递归的过程。
274+
275+
与方法一类似,我们首先判断 `root` 是否为空,如果为空则返回 $0$。
276+
277+
否则,我们初始化答案变量 $ans$ 为 $0$,然后初始化栈 $stk$,将 `root` 加入栈中。
278+
279+
当栈不为空时,我们弹出栈顶元素 `root`,如果 `root` 的左子节点存在,我们判断其是否为叶子节点,如果是叶子节点,则将其值加到答案变量 $ans$ 中,否则我们将其左子节点加入栈中。然后我们判断 `root` 的右子节点是否存在,如果存在,我们将其加入栈中。
280+
281+
最后返回答案即可。
282+
283+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。
256284
257285
<!-- tabs:start -->
258286
287+
```python
288+
# Definition for a binary tree node.
289+
# class TreeNode:
290+
# def __init__(self, val=0, left=None, right=None):
291+
# self.val = val
292+
# self.left = left
293+
# self.right = right
294+
class Solution:
295+
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
296+
if root is None:
297+
return 0
298+
ans = 0
299+
stk = [root]
300+
while stk:
301+
root = stk.pop()
302+
if root.left:
303+
if root.left.left == root.left.right:
304+
ans += root.left.val
305+
else:
306+
stk.append(root.left)
307+
if root.right:
308+
stk.append(root.right)
309+
return ans
310+
```
311+
312+
```java
313+
/**
314+
* Definition for a binary tree node.
315+
* public class TreeNode {
316+
* int val;
317+
* TreeNode left;
318+
* TreeNode right;
319+
* TreeNode() {}
320+
* TreeNode(int val) { this.val = val; }
321+
* TreeNode(int val, TreeNode left, TreeNode right) {
322+
* this.val = val;
323+
* this.left = left;
324+
* this.right = right;
325+
* }
326+
* }
327+
*/
328+
class Solution {
329+
public int sumOfLeftLeaves(TreeNode root) {
330+
if (root == null) {
331+
return 0;
332+
}
333+
Deque<TreeNode> stk = new ArrayDeque<>();
334+
stk.push(root);
335+
int ans = 0;
336+
while (!stk.isEmpty()) {
337+
root = stk.pop();
338+
if (root.left != null) {
339+
if (root.left.left == root.left.right) {
340+
ans += root.left.val;
341+
} else {
342+
stk.push(root.left);
343+
}
344+
}
345+
if (root.right != null) {
346+
stk.push(root.right);
347+
}
348+
}
349+
return ans;
350+
}
351+
}
352+
```
353+
259354
```cpp
260355
/**
261356
* Definition for a binary tree node.
@@ -294,6 +389,76 @@ public:
294389
};
295390
```
296391
392+
```go
393+
/**
394+
* Definition for a binary tree node.
395+
* type TreeNode struct {
396+
* Val int
397+
* Left *TreeNode
398+
* Right *TreeNode
399+
* }
400+
*/
401+
func sumOfLeftLeaves(root *TreeNode) (ans int) {
402+
if root == nil {
403+
return 0
404+
}
405+
stk := []*TreeNode{root}
406+
for len(stk) > 0 {
407+
root = stk[len(stk)-1]
408+
stk = stk[:len(stk)-1]
409+
if root.Left != nil {
410+
if root.Left.Left == root.Left.Right {
411+
ans += root.Left.Val
412+
} else {
413+
stk = append(stk, root.Left)
414+
}
415+
}
416+
if root.Right != nil {
417+
stk = append(stk, root.Right)
418+
}
419+
}
420+
return
421+
}
422+
```
423+
424+
```ts
425+
/**
426+
* Definition for a binary tree node.
427+
* class TreeNode {
428+
* val: number
429+
* left: TreeNode | null
430+
* right: TreeNode | null
431+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
432+
* this.val = (val===undefined ? 0 : val)
433+
* this.left = (left===undefined ? null : left)
434+
* this.right = (right===undefined ? null : right)
435+
* }
436+
* }
437+
*/
438+
439+
function sumOfLeftLeaves(root: TreeNode | null): number {
440+
if (!root) {
441+
return 0;
442+
}
443+
let ans = 0;
444+
const stk: TreeNode[] = [root];
445+
while (stk.length) {
446+
const { left, right } = stk.pop()!;
447+
if (left) {
448+
if (left.left === left.right) {
449+
ans += left.val;
450+
} else {
451+
stk.push(left);
452+
}
453+
}
454+
if (right) {
455+
stk.push(right);
456+
}
457+
}
458+
return ans;
459+
}
460+
```
461+
297462
<!-- tabs:end -->
298463

299464
<!-- end -->

0 commit comments

Comments
 (0)