Skip to content

Commit 965202d

Browse files
committed
feat: add solutions to lc problem: No.0513. Find Bottom Left Tree Value
1 parent 03159c0 commit 965202d

File tree

6 files changed

+162
-29
lines changed

6 files changed

+162
-29
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@
195195

196196
## 维护者
197197

198-
[Yang Libin](https://github.com/yanglbme): 开源社区 @Doocs 创建者「[GitHub](https://github.com/doocs) / [Gitee](https://gitee.com/doocs)」;GitHub 开源组织 [@TheAlgorithms](https://github.com/TheAlgorithms) 成员。
198+
- [Yang Libin](https://github.com/yanglbme)
199+
- [Mao Longlong](https://github.com/MaoLongLong)
200+
199201

200202
## 加入我们
201203

README_EN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
188188

189189
## Maintainer
190190

191-
[Yang Libin](https://github.com/yanglbme): Creator of [@Doocs](https://github.com/doocs) organization; member of [@TheAlgorithms](https://github.com/TheAlgorithms) organization.
191+
- [Yang Libin](https://github.com/yanglbme)
192+
- [Mao Longlong](https://github.com/MaoLongLong)
192193

193194
## Contributions
194195

solution/0500-0599/0513.Find Bottom Left Tree Value/README.md

+57-3
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,81 @@
4444

4545
<p><strong>注意:</strong> 您可以假设树(即给定的根节点)不为 <strong>NULL</strong>。</p>
4646

47-
4847
## 解法
4948

5049
<!-- 这里可写通用的实现逻辑 -->
5150

51+
“BFS 层次遍历”实现。
52+
5253
<!-- tabs:start -->
5354

5455
### **Python3**
5556

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

5859
```python
59-
60+
# Definition for a binary tree node.
61+
# class TreeNode:
62+
# def __init__(self, val=0, left=None, right=None):
63+
# self.val = val
64+
# self.left = left
65+
# self.right = right
66+
class Solution:
67+
def findBottomLeftValue(self, root: TreeNode) -> int:
68+
res = 0
69+
q = collections.deque([root])
70+
while q:
71+
res = q[0].val
72+
n = len(q)
73+
for _ in range(n):
74+
node = q.popleft()
75+
if node.left:
76+
q.append(node.left)
77+
if node.right:
78+
q.append(node.right)
79+
return res
6080
```
6181

6282
### **Java**
6383

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

6686
```java
67-
87+
/**
88+
* Definition for a binary tree node.
89+
* public class TreeNode {
90+
* int val;
91+
* TreeNode left;
92+
* TreeNode right;
93+
* TreeNode() {}
94+
* TreeNode(int val) { this.val = val; }
95+
* TreeNode(int val, TreeNode left, TreeNode right) {
96+
* this.val = val;
97+
* this.left = left;
98+
* this.right = right;
99+
* }
100+
* }
101+
*/
102+
class Solution {
103+
public int findBottomLeftValue(TreeNode root) {
104+
Deque<TreeNode> q = new ArrayDeque<>();
105+
q.offer(root);
106+
int res = 0;
107+
while (!q.isEmpty()) {
108+
res = q.peek().val;
109+
for (int i = 0, n = q.size(); i < n; ++i) {
110+
TreeNode node = q.poll();
111+
if (node.left != null) {
112+
q.offer(node.left);
113+
}
114+
if (node.right != null) {
115+
q.offer(node.right);
116+
}
117+
}
118+
}
119+
return res;
120+
}
121+
}
68122
```
69123

70124
### **...**

solution/0500-0599/0513.Find Bottom Left Tree Value/README_EN.md

+55-3
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,73 @@
2929
<li><code>-2<sup>31</sup> &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>
3030
</ul>
3131

32-
3332
## Solutions
3433

3534
<!-- tabs:start -->
3635

3736
### **Python3**
3837

3938
```python
40-
39+
# Definition for a binary tree node.
40+
# class TreeNode:
41+
# def __init__(self, val=0, left=None, right=None):
42+
# self.val = val
43+
# self.left = left
44+
# self.right = right
45+
class Solution:
46+
def findBottomLeftValue(self, root: TreeNode) -> int:
47+
res = 0
48+
q = collections.deque([root])
49+
while q:
50+
res = q[0].val
51+
n = len(q)
52+
for _ in range(n):
53+
node = q.popleft()
54+
if node.left:
55+
q.append(node.left)
56+
if node.right:
57+
q.append(node.right)
58+
return res
4159
```
4260

4361
### **Java**
4462

4563
```java
46-
64+
/**
65+
* Definition for a binary tree node.
66+
* public class TreeNode {
67+
* int val;
68+
* TreeNode left;
69+
* TreeNode right;
70+
* TreeNode() {}
71+
* TreeNode(int val) { this.val = val; }
72+
* TreeNode(int val, TreeNode left, TreeNode right) {
73+
* this.val = val;
74+
* this.left = left;
75+
* this.right = right;
76+
* }
77+
* }
78+
*/
79+
class Solution {
80+
public int findBottomLeftValue(TreeNode root) {
81+
Deque<TreeNode> q = new ArrayDeque<>();
82+
q.offer(root);
83+
int res = 0;
84+
while (!q.isEmpty()) {
85+
res = q.peek().val;
86+
for (int i = 0, n = q.size(); i < n; ++i) {
87+
TreeNode node = q.poll();
88+
if (node.left != null) {
89+
q.offer(node.left);
90+
}
91+
if (node.right != null) {
92+
q.offer(node.right);
93+
}
94+
}
95+
}
96+
return res;
97+
}
98+
}
4799
```
48100

49101
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
/**
22
* Definition for a binary tree node.
33
* public class TreeNode {
4-
* int val;
5-
* TreeNode left;
6-
* TreeNode right;
7-
* TreeNode(int x) { val = x; }
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
814
* }
915
*/
1016
class Solution {
11-
int max = -1;
12-
int value = 0;
13-
1417
public int findBottomLeftValue(TreeNode root) {
15-
dfs(root, 0);
16-
return value;
17-
}
18-
19-
private void dfs(TreeNode root, int d) {
20-
if (root == null) {
21-
return;
22-
}
23-
d++;
24-
if (max < d) {
25-
max = d;
26-
value = root.val;
18+
Deque<TreeNode> q = new ArrayDeque<>();
19+
q.offer(root);
20+
int res = 0;
21+
while (!q.isEmpty()) {
22+
res = q.peek().val;
23+
for (int i = 0, n = q.size(); i < n; ++i) {
24+
TreeNode node = q.poll();
25+
if (node.left != null) {
26+
q.offer(node.left);
27+
}
28+
if (node.right != null) {
29+
q.offer(node.right);
30+
}
31+
}
2732
}
28-
dfs(root.left, d);
29-
dfs(root.right, d);
33+
return res;
3034
}
3135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def findBottomLeftValue(self, root: TreeNode) -> int:
9+
res = 0
10+
q = collections.deque([root])
11+
while q:
12+
res = q[0].val
13+
n = len(q)
14+
for _ in range(n):
15+
node = q.popleft()
16+
if node.left:
17+
q.append(node.left)
18+
if node.right:
19+
q.append(node.right)
20+
return res

0 commit comments

Comments
 (0)