@@ -202,7 +202,7 @@ const bfs = function(root){
202
202
const queue = [];
203
203
queue .push (root);
204
204
while (queue .length !== 0 ){
205
- let node = queue .shift ();
205
+ const node = queue .shift ();
206
206
res .push (node .val );
207
207
if (node .left !== null ) {
208
208
queue .push (node .left );
@@ -215,7 +215,7 @@ const bfs = function(root){
215
215
}
216
216
```
217
217
218
- ##### 104.[ 二叉树的最大深度] ( https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ )
218
+ [ 104.二叉树的最大深度] ( https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ )
219
219
220
220
给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。** 说明:** 叶子节点是指没有子节点的节点。
221
221
@@ -238,7 +238,7 @@ const maxDepth = function(root) { //递归
238
238
};
239
239
```
240
240
241
- 110.[ 平衡二叉树] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
241
+ [ 110.平衡二叉树] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
242
242
243
243
给定一个二叉树,判断它是否是高度平衡的二叉树。
244
244
@@ -268,16 +268,12 @@ const maxDepth = function(root) {
268
268
}
269
269
```
270
270
271
- 124.[ 二叉树中的最大路径和] ( https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/ )
271
+ [ 124.二叉树中的最大路径和] ( https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/ )
272
272
273
273
** 路径** 被定义为一条从树中任意节点出发,沿父节点—子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 ** 至多出现一次** 。该路径 ** 至少包含一个** 节点,且不一定经过根节点。
274
-
275
274
** 路径和** 是路径中各节点值的总和。
276
-
277
275
给你一个二叉树的根节点 ` root ` ,返回其 ** 最大路径和** 。
278
276
279
- > 思路:分治法,分为三种情况:左子树最大路径和最大,右子树最大路径和最大,左右子树最大加根节点最大,需要保存两个变量:一个保存子树最大路径和,一个保存左右加根节点和,然后比较这个两个变量选择最大值即可
280
-
281
277
``` js
282
278
var maxPathSum = function (root ) {
283
279
let maxSum = Number .MIN_SAFE_INTEGER
@@ -295,3 +291,169 @@ var maxPathSum = function (root) {
295
291
};
296
292
```
297
293
294
+ [ 236. 二叉树的最近公共祖先] ( https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ )
295
+
296
+ 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
297
+
298
+ ``` js
299
+ var lowestCommonAncestor = function (root , p , q ) {
300
+ let ans = root
301
+ const dfs = (node ) => {
302
+ if (node === null ) {
303
+ return false
304
+ }
305
+ const lson = dfs (node .left )
306
+ const rson = dfs (node .right )
307
+ if (((node .val === p .val || node .val === q .val ) && (lson || rson)) || (lson && rson)) {
308
+ ans = node
309
+ }
310
+ return (node .val === p .val || node .val === q .val ) || lson || rson
311
+ }
312
+ dfs (root)
313
+ return ans
314
+ };
315
+ ```
316
+
317
+ [ 102. 二叉树的层序遍历] ( https://leetcode-cn.com/problems/binary-tree-level-order-traversal/ )
318
+
319
+ 给你一个二叉树,请你返回其按 ** 层序遍历** 得到的节点值。
320
+
321
+ ``` js
322
+ var levelOrder = function (root ) {
323
+ if (root === null ) {
324
+ return []
325
+ }
326
+ const res = []
327
+ const queue = []
328
+ queue .push (root)
329
+ while (queue .length > 0 ) {
330
+ const currentQueueSize = queue .length
331
+ const list = [] // 存放每一层的节点值
332
+ for (let i = 1 ; i <= currentQueueSize; i++ ) {
333
+ const node = queue .shift ()
334
+ list .push (node .val )
335
+ if (node .left ) {
336
+ queue .push (node .left )
337
+ }
338
+ if (node .right ) {
339
+ queue .push (node .right )
340
+ }
341
+ }
342
+ res .push (list)
343
+ }
344
+ return res
345
+ };
346
+ ```
347
+
348
+ [ 107. 二叉树的层序遍历 II] ( https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/ )
349
+
350
+ 给定一个二叉树,返回其节点值自底向上的层次遍历。(即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
351
+
352
+ ``` js
353
+ if (root === null ) {
354
+ return []
355
+ }
356
+ const res = []
357
+ const queue = []
358
+ queue .push (root)
359
+ while (queue .length > 0 ) {
360
+ const currentQueueSize = queue .length
361
+ const list = []
362
+ for (let i = 1 ; i <= currentQueueSize; i++ ) {
363
+ const node = queue .shift ()
364
+ list .push (node .val )
365
+ if (node .left ) {
366
+ queue .push (node .left )
367
+ }
368
+ if (node .right ) {
369
+ queue .push (node .right )
370
+ }
371
+ }
372
+ res .unshift (list) // unshift 从头部插入
373
+ }
374
+ return res
375
+ ```
376
+
377
+ [ 103. 二叉树的锯齿形层序遍历] ( https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/ )
378
+
379
+ 给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
380
+
381
+ ``` js
382
+ var zigzagLevelOrder = function (root ) {
383
+ if (root === null ) {
384
+ return []
385
+ }
386
+ const res = []
387
+ const queue = []
388
+ queue .push (root)
389
+ while (queue .length > 0 ) {
390
+ const currentQueueSize = queue .length
391
+ const list = []
392
+ for (let i = 1 ; i <= currentQueueSize; i++ ) {
393
+ const node = queue .shift ()
394
+ list .push (node .val )
395
+ if (node .left ) {
396
+ queue .push (node .left )
397
+ }
398
+ if (node .right ) {
399
+ queue .push (node .right )
400
+ }
401
+ }
402
+ res .push (list)
403
+ }
404
+ return res .map ((item , index ) => {
405
+ if (index % 2 === 1 ) {
406
+ item = item .reverse ()
407
+ }
408
+ return item
409
+ })
410
+ };
411
+ ```
412
+
413
+ [ 98. 验证二叉搜索树] ( https://leetcode-cn.com/problems/validate-binary-search-tree/ )
414
+
415
+ 给定一个二叉树,判断其是否是一个有效的二叉搜索树。
416
+
417
+ 假设一个二叉搜索树具有如下特征:
418
+ 节点的左子树只包含小于当前节点的数。
419
+ 节点的右子树只包含大于当前节点的数。
420
+ 所有左子树和右子树自身必须也是二叉搜索树。
421
+
422
+ ``` js
423
+ var isValidBST = function (root ) { // 中序遍历,值会从小到大排列
424
+ const list = []
425
+ inOrder (root, list)
426
+ for (let i= 0 ;i< list .length ;i++ ){
427
+ if (list[i] >= list[i+ 1 ]){
428
+ return false
429
+ }
430
+ }
431
+ return true
432
+ };
433
+
434
+ const inOrder = (node , list ) => {
435
+ if (node === null ) {
436
+ return null
437
+ }
438
+ const left = inOrder (node .left ,list)
439
+ list .push (node .val )
440
+ const right = inOrder (node .right ,list)
441
+ }
442
+ ```
443
+
444
+ ``` js
445
+ var isValidBST = function (root ) { // 递归写法
446
+ return helper (root, - Infinity , Infinity )
447
+ };
448
+
449
+ const helper = (node , smaller , bigger ) => {
450
+ if (node === null ) {
451
+ return true
452
+ }
453
+ if (node .val <= smaller || node .val >= bigger) {
454
+ return false
455
+ }
456
+ return helper (node .left , smaller, node .val ) && helper (node .right , node .val , bigger)
457
+ }
458
+ ```
459
+
0 commit comments