Skip to content

Commit 6ec4f22

Browse files
committed
feat: add solutions to lc problem: No.0513
No.0513.Find Bottom Left Tree Value
1 parent a00feb7 commit 6ec4f22

File tree

7 files changed

+396
-103
lines changed

7 files changed

+396
-103
lines changed

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

Lines changed: 189 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
“BFS 层次遍历”实现。
46+
**方法一:BFS**
47+
48+
BFS 找最后一层第一个节点。
49+
50+
**方法二:DFS**
51+
52+
DFS 先序遍历,找深度最大的,且第一次被遍历到的节点。
4753

4854
<!-- tabs:start -->
4955

@@ -59,22 +65,44 @@
5965
# self.left = left
6066
# self.right = right
6167
class Solution:
62-
def findBottomLeftValue(self, root: TreeNode) -> int:
68+
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
6369
q = deque([root])
64-
ans = -1
70+
ans = 0
6571
while q:
66-
n = len(q)
67-
for i in range(n):
72+
ans = q[0].val
73+
for _ in range(len(q)):
6874
node = q.popleft()
69-
if i == 0:
70-
ans = node.val
7175
if node.left:
7276
q.append(node.left)
7377
if node.right:
7478
q.append(node.right)
7579
return ans
7680
```
7781

82+
```python
83+
# Definition for a binary tree node.
84+
# class TreeNode:
85+
# def __init__(self, val=0, left=None, right=None):
86+
# self.val = val
87+
# self.left = left
88+
# self.right = right
89+
class Solution:
90+
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
91+
def dfs(root, curr):
92+
if root is None:
93+
return
94+
dfs(root.left, curr + 1)
95+
dfs(root.right, curr + 1)
96+
nonlocal ans, mx
97+
if mx < curr:
98+
mx = curr
99+
ans = root.val
100+
101+
ans = mx = 0
102+
dfs(root, 1)
103+
return ans
104+
```
105+
78106
### **Java**
79107

80108
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -99,14 +127,11 @@ class Solution {
99127
public int findBottomLeftValue(TreeNode root) {
100128
Queue<TreeNode> q = new ArrayDeque<>();
101129
q.offer(root);
102-
int ans = -1;
130+
int ans = 0;
103131
while (!q.isEmpty()) {
104-
int n = q.size();
105-
for (int i = 0; i < n; i++) {
132+
ans = q.peek().val;
133+
for (int i = q.size(); i > 0; --i) {
106134
TreeNode node = q.poll();
107-
if (i == 0) {
108-
ans = node.val;
109-
}
110135
if (node.left != null) {
111136
q.offer(node.left);
112137
}
@@ -120,6 +145,45 @@ class Solution {
120145
}
121146
```
122147

148+
```java
149+
/**
150+
* Definition for a binary tree node.
151+
* public class TreeNode {
152+
* int val;
153+
* TreeNode left;
154+
* TreeNode right;
155+
* TreeNode() {}
156+
* TreeNode(int val) { this.val = val; }
157+
* TreeNode(int val, TreeNode left, TreeNode right) {
158+
* this.val = val;
159+
* this.left = left;
160+
* this.right = right;
161+
* }
162+
* }
163+
*/
164+
class Solution {
165+
private int ans = 0;
166+
private int mx = 0;
167+
168+
public int findBottomLeftValue(TreeNode root) {
169+
dfs(root, 1);
170+
return ans;
171+
}
172+
173+
private void dfs(TreeNode root, int curr) {
174+
if (root == null) {
175+
return;
176+
}
177+
dfs(root.left, curr + 1);
178+
dfs(root.right, curr + 1);
179+
if (mx < curr) {
180+
mx = curr;
181+
ans = root.val;
182+
}
183+
}
184+
}
185+
```
186+
123187
### **TypeScript**
124188

125189
```ts
@@ -138,23 +202,55 @@ class Solution {
138202
*/
139203

140204
function findBottomLeftValue(root: TreeNode | null): number {
141-
let stack: Array<TreeNode> = [root];
142-
let ans = root.val;
143-
while (stack.length) {
144-
let next = [];
145-
for (let node of stack) {
205+
let ans = 0;
206+
const q = [root];
207+
while (q.length) {
208+
ans = q[0].val;
209+
for (let i = q.length; i; --i) {
210+
const node = q.shift();
146211
if (node.left) {
147-
next.push(node.left);
212+
q.push(node.left);
148213
}
149214
if (node.right) {
150-
next.push(node.right);
215+
q.push(node.right);
151216
}
152217
}
153-
if (next.length) {
154-
ans = next[0].val;
218+
}
219+
return ans;
220+
}
221+
```
222+
223+
```ts
224+
/**
225+
* Definition for a binary tree node.
226+
* class TreeNode {
227+
* val: number
228+
* left: TreeNode | null
229+
* right: TreeNode | null
230+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
231+
* this.val = (val===undefined ? 0 : val)
232+
* this.left = (left===undefined ? null : left)
233+
* this.right = (right===undefined ? null : right)
234+
* }
235+
* }
236+
*/
237+
238+
function findBottomLeftValue(root: TreeNode | null): number {
239+
let mx = 0;
240+
let ans = 0;
241+
242+
function dfs(root, curr) {
243+
if (!root) {
244+
return;
245+
}
246+
dfs(root.left, curr + 1);
247+
dfs(root.right, curr + 1);
248+
if (mx < curr) {
249+
mx = curr;
250+
ans = root.val;
155251
}
156-
stack = next;
157252
}
253+
dfs(root, 1);
158254
return ans;
159255
}
160256
```
@@ -176,15 +272,14 @@ function findBottomLeftValue(root: TreeNode | null): number {
176272
class Solution {
177273
public:
178274
int findBottomLeftValue(TreeNode* root) {
179-
queue<TreeNode*> q;
180-
q.push(root);
181-
int ans = -1;
275+
queue<TreeNode*> q{{root}};
276+
int ans = 0;
182277
while (!q.empty())
183278
{
184-
for (int i = 0, n = q.size(); i < n; ++i)
279+
ans = q.front()->val;
280+
for (int i = q.size(); i; --i)
185281
{
186282
TreeNode* node = q.front();
187-
if (i == 0) ans = node->val;
188283
q.pop();
189284
if (node->left) q.push(node->left);
190285
if (node->right) q.push(node->right);
@@ -195,6 +290,40 @@ public:
195290
};
196291
```
197292
293+
```cpp
294+
/**
295+
* Definition for a binary tree node.
296+
* struct TreeNode {
297+
* int val;
298+
* TreeNode *left;
299+
* TreeNode *right;
300+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
301+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
302+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
303+
* };
304+
*/
305+
class Solution {
306+
public:
307+
int ans = 0;
308+
int mx = 0;
309+
int findBottomLeftValue(TreeNode* root) {
310+
dfs(root, 1);
311+
return ans;
312+
}
313+
314+
void dfs(TreeNode* root, int curr) {
315+
if (!root) return;
316+
dfs(root->left, curr + 1);
317+
dfs(root->right, curr + 1);
318+
if (mx < curr)
319+
{
320+
mx = curr;
321+
ans = root->val;
322+
}
323+
}
324+
};
325+
```
326+
198327
### **Go**
199328

200329
```go
@@ -208,14 +337,12 @@ public:
208337
*/
209338
func findBottomLeftValue(root *TreeNode) int {
210339
q := []*TreeNode{root}
211-
ans := -1
212-
for n := len(q); n > 0; n = len(q) {
213-
for i := 0; i < n; i++ {
340+
ans := 0
341+
for len(q) > 0 {
342+
ans = q[0].Val
343+
for i := len(q); i > 0; i-- {
214344
node := q[0]
215345
q = q[1:]
216-
if i == 0 {
217-
ans = node.Val
218-
}
219346
if node.Left != nil {
220347
q = append(q, node.Left)
221348
}
@@ -228,6 +355,34 @@ func findBottomLeftValue(root *TreeNode) int {
228355
}
229356
```
230357

358+
```go
359+
/**
360+
* Definition for a binary tree node.
361+
* type TreeNode struct {
362+
* Val int
363+
* Left *TreeNode
364+
* Right *TreeNode
365+
* }
366+
*/
367+
func findBottomLeftValue(root *TreeNode) int {
368+
ans, mx := 0, 0
369+
var dfs func(*TreeNode, int)
370+
dfs = func(root *TreeNode, curr int) {
371+
if root == nil {
372+
return
373+
}
374+
dfs(root.Left, curr+1)
375+
dfs(root.Right, curr+1)
376+
if mx < curr {
377+
mx = curr
378+
ans = root.Val
379+
}
380+
}
381+
dfs(root, 1)
382+
return ans
383+
}
384+
```
385+
231386
### **...**
232387

233388
```

0 commit comments

Comments
 (0)