Skip to content

Commit ad6205e

Browse files
authored
feat: add solutions to lc problem: No.0404 (#2607)
1 parent 68b4860 commit ad6205e

File tree

11 files changed

+599
-139
lines changed

11 files changed

+599
-139
lines changed

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

+211-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,107 @@ impl Solution {
235249
* };
236250
*/
237251

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

299463
<!-- end -->

0 commit comments

Comments
 (0)