Skip to content

Commit 549f3e8

Browse files
committedMar 28, 2021
feat: update leetcode solutions: No.0404. Sum of Left Leaves
1 parent bccba45 commit 549f3e8

File tree

6 files changed

+109
-16
lines changed

6 files changed

+109
-16
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
- [二分查找](/basic/searching/BinarySearch/README.md)
4646
- [二分查找 II](/basic/searching/BinarySearch-II/README.md)
4747

48-
## 面试高频考题
48+
## 高频考题
4949

5050
### 数组
5151

‎index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
<head>
55
<meta charset="UTF-8">
6-
<title>LeetCode、剑指Offer、程序员面试金典题解</title>
6+
<title>LeetCode & Coding Interview Guide</title>
77
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
88
<meta name="keywords"
9-
content="doc,docs,doocs,documentation,github,gitee,coding,pages,leetcode,coding-interview,cracking-the-coding-interview,yanglbme">
9+
content="doc,docs,doocs,documentation,github,gitee,coding,pages,leetcode,coding-interview,coding-interview-guide,cracking-the-coding-interview,yanglbme">
1010
<meta name="description" content="LeetCode、剑指Offer、程序员面试金典题解">
1111
<meta name="viewport"
1212
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
@@ -22,7 +22,7 @@
2222
</head>
2323

2424
<body>
25-
<div id="app">本系列知识由 Doocs 技术社区原创发布</div>
25+
<div id="app">LeetCode & Coding Interview Guide</div>
2626
<script>
2727
window.$docsify = {
2828
name: 'leetcode',

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

+40-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,53 @@
3131
<!-- 这里可写当前语言的特殊实现逻辑 -->
3232

3333
```python
34-
34+
# Definition for a binary tree node.
35+
# class TreeNode:
36+
# def __init__(self, x):
37+
# self.val = x
38+
# self.left = None
39+
# self.right = None
40+
41+
class Solution:
42+
def sumOfLeftLeaves(self, root: TreeNode) -> int:
43+
if root is None:
44+
return 0
45+
res = 0
46+
if root.left and root.left.left is None and root.left.right is None:
47+
res += root.left.val
48+
res += self.sumOfLeftLeaves(root.left)
49+
res += self.sumOfLeftLeaves(root.right)
50+
return res
3551
```
3652

3753
### **Java**
3854

3955
<!-- 这里可写当前语言的特殊实现逻辑 -->
4056

4157
```java
42-
58+
/**
59+
* Definition for a binary tree node.
60+
* public class TreeNode {
61+
* int val;
62+
* TreeNode left;
63+
* TreeNode right;
64+
* TreeNode(int x) { val = x; }
65+
* }
66+
*/
67+
class Solution {
68+
public int sumOfLeftLeaves(TreeNode root) {
69+
if (root == null) {
70+
return 0;
71+
}
72+
int res = 0;
73+
if (root.left != null && root.left.left == null && root.left.right == null) {
74+
res += root.left.val;
75+
}
76+
res += sumOfLeftLeaves(root.left);
77+
res += sumOfLeftLeaves(root.right);
78+
return res;
79+
}
80+
}
4381
```
4482

4583
### **...**

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

+40-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,51 @@ There are two left leaves in the binary tree, with values <b>9</b> and <b>15</b>
3535
### **Python3**
3636

3737
```python
38-
38+
# Definition for a binary tree node.
39+
# class TreeNode:
40+
# def __init__(self, x):
41+
# self.val = x
42+
# self.left = None
43+
# self.right = None
44+
45+
class Solution:
46+
def sumOfLeftLeaves(self, root: TreeNode) -> int:
47+
if root is None:
48+
return 0
49+
res = 0
50+
if root.left and root.left.left is None and root.left.right is None:
51+
res += root.left.val
52+
res += self.sumOfLeftLeaves(root.left)
53+
res += self.sumOfLeftLeaves(root.right)
54+
return res
3955
```
4056

4157
### **Java**
4258

4359
```java
44-
60+
/**
61+
* Definition for a binary tree node.
62+
* public class TreeNode {
63+
* int val;
64+
* TreeNode left;
65+
* TreeNode right;
66+
* TreeNode(int x) { val = x; }
67+
* }
68+
*/
69+
class Solution {
70+
public int sumOfLeftLeaves(TreeNode root) {
71+
if (root == null) {
72+
return 0;
73+
}
74+
int res = 0;
75+
if (root.left != null && root.left.left == null && root.left.right == null) {
76+
res += root.left.val;
77+
}
78+
res += sumOfLeftLeaves(root.left);
79+
res += sumOfLeftLeaves(root.right);
80+
return res;
81+
}
82+
}
4583
```
4684

4785
### **...**

‎solution/0400-0499/0404.Sum of Left Leaves/Solution.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
* }
99
*/
1010
class Solution {
11-
12-
private int sum = 0;
13-
1411
public int sumOfLeftLeaves(TreeNode root) {
15-
if (root == null) return 0;
12+
if (root == null) {
13+
return 0;
14+
}
15+
int res = 0;
1616
if (root.left != null && root.left.left == null && root.left.right == null) {
17-
sum += root.left.val;
17+
res += root.left.val;
1818
}
19-
sumOfLeftLeaves(root.left);
20-
sumOfLeftLeaves(root.right);
21-
return sum;
19+
res += sumOfLeftLeaves(root.left);
20+
res += sumOfLeftLeaves(root.right);
21+
return res;
2222
}
2323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def sumOfLeftLeaves(self, root: TreeNode) -> int:
10+
if root is None:
11+
return 0
12+
res = 0
13+
if root.left and root.left.left is None and root.left.right is None:
14+
res += root.left.val
15+
res += self.sumOfLeftLeaves(root.left)
16+
res += self.sumOfLeftLeaves(root.right)
17+
return res

0 commit comments

Comments
 (0)