43
43
44
44
<!-- 这里可写通用的实现逻辑 -->
45
45
46
- “BFS 层次遍历”实现。
46
+ ** 方法一:BFS**
47
+
48
+ BFS 找最后一层第一个节点。
49
+
50
+ ** 方法二:DFS**
51
+
52
+ DFS 先序遍历,找深度最大的,且第一次被遍历到的节点。
47
53
48
54
<!-- tabs:start -->
49
55
59
65
# self.left = left
60
66
# self.right = right
61
67
class Solution :
62
- def findBottomLeftValue (self , root : TreeNode) -> int :
68
+ def findBottomLeftValue (self , root : Optional[ TreeNode] ) -> int :
63
69
q = deque([root])
64
- ans = - 1
70
+ ans = 0
65
71
while q:
66
- n = len (q)
67
- for i in range (n ):
72
+ ans = q[ 0 ].val
73
+ for _ in range (len (q) ):
68
74
node = q.popleft()
69
- if i == 0 :
70
- ans = node.val
71
75
if node.left:
72
76
q.append(node.left)
73
77
if node.right:
74
78
q.append(node.right)
75
79
return ans
76
80
```
77
81
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
+
78
106
### ** Java**
79
107
80
108
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -99,14 +127,11 @@ class Solution {
99
127
public int findBottomLeftValue (TreeNode root ) {
100
128
Queue<TreeNode > q = new ArrayDeque<> ();
101
129
q. offer(root);
102
- int ans = - 1 ;
130
+ int ans = 0 ;
103
131
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 ) {
106
134
TreeNode node = q. poll();
107
- if (i == 0 ) {
108
- ans = node. val;
109
- }
110
135
if (node. left != null ) {
111
136
q. offer(node. left);
112
137
}
@@ -120,6 +145,45 @@ class Solution {
120
145
}
121
146
```
122
147
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
+
123
187
### ** TypeScript**
124
188
125
189
``` ts
@@ -138,23 +202,55 @@ class Solution {
138
202
*/
139
203
140
204
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 ();
146
211
if (node .left ) {
147
- next .push (node .left );
212
+ q .push (node .left );
148
213
}
149
214
if (node .right ) {
150
- next .push (node .right );
215
+ q .push (node .right );
151
216
}
152
217
}
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 ;
155
251
}
156
- stack = next ;
157
252
}
253
+ dfs (root , 1 );
158
254
return ans ;
159
255
}
160
256
```
@@ -176,15 +272,14 @@ function findBottomLeftValue(root: TreeNode | null): number {
176
272
class Solution {
177
273
public:
178
274
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;
182
277
while (!q.empty())
183
278
{
184
- for (int i = 0, n = q.size(); i < n; ++i)
279
+ ans = q.front()->val;
280
+ for (int i = q.size(); i; --i)
185
281
{
186
282
TreeNode* node = q.front();
187
- if (i == 0) ans = node->val;
188
283
q.pop();
189
284
if (node->left) q.push(node->left);
190
285
if (node->right) q.push(node->right);
@@ -195,6 +290,40 @@ public:
195
290
};
196
291
```
197
292
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
+
198
327
### ** Go**
199
328
200
329
``` go
@@ -208,14 +337,12 @@ public:
208
337
*/
209
338
func findBottomLeftValue (root *TreeNode ) int {
210
339
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-- {
214
344
node := q[0 ]
215
345
q = q[1 :]
216
- if i == 0 {
217
- ans = node.Val
218
- }
219
346
if node.Left != nil {
220
347
q = append (q, node.Left )
221
348
}
@@ -228,6 +355,34 @@ func findBottomLeftValue(root *TreeNode) int {
228
355
}
229
356
```
230
357
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
+
231
386
### ** ...**
232
387
233
388
```
0 commit comments