@@ -235,7 +235,7 @@ class Solution:
235
235
# 可以递归内部函数,也可以递归自己
236
236
```
237
237
238
- 或者反过来计算深度:
238
+ 或者反过来计算深度:(但是,在下一道判断平衡二叉树时,还是叶子节点从0起返回更好)
239
239
``` python
240
240
class Solution :
241
241
def maxDepth (self , root : TreeNode) -> int :
@@ -246,7 +246,7 @@ class Solution:
246
246
return depth(root, 0 )
247
247
```
248
248
249
- - 思路 2:层序遍历
249
+ - 思路 2:层序遍历(略过了)
250
250
251
251
``` Python
252
252
class Solution :
@@ -340,14 +340,14 @@ class Solution:
340
340
341
341
def largest_path_ends_at (node ):
342
342
if node is None :
343
- return float (' -inf' )
343
+ return float (' -inf' ) # 记住这里返回-inf而非0!例如输入[-3],左右向答案的贡献变成了0
344
344
345
345
e_l = largest_path_ends_at(node.left)
346
346
e_r = largest_path_ends_at(node.right)
347
347
348
348
self .maxPath = max (self .maxPath, node.val + max (0 , e_l) + max (0 , e_r), e_l, e_r)
349
349
350
- return node.val + max (e_l, e_r, 0 )
350
+ return node.val + max (e_l, e_r, 0 ) # 这里有个0,处理叶子节点返回值
351
351
352
352
largest_path_ends_at(root)
353
353
return self .maxPath
@@ -400,7 +400,7 @@ class Solution:
400
400
level = collections.deque([])
401
401
append_this = level.append
402
402
403
- while len ( bfs) > 0 :
403
+ while bfs:
404
404
level_size = len (bfs)
405
405
for _ in range (level_size):
406
406
node = bfs.popleft()
@@ -445,61 +445,29 @@ class Solution:
445
445
446
446
- 利用性质:BST中序遍历应得到升序序列。递归中序遍历仍不能提前输出,故用非递归遍历的模板。
447
447
448
- TODO:
449
-
450
-
451
- - 思路 2:分治法,一个二叉树为合法的二叉搜索树当且仅当左右子树为合法二叉搜索树且根结点值大于右子树最小值小于左子树最大值。缺点是若不用迭代形式实现则无法提前返回,而迭代实现右比较复杂。
452
-
453
- ``` Python
448
+ ``` python
454
449
class Solution :
455
450
def isValidBST (self , root : TreeNode) -> bool :
456
-
457
- if root is None : return True
458
-
459
- def valid_min_max (node ):
460
-
461
- isValid = True
462
- if node.left is not None :
463
- l_isValid, l_min, l_max = valid_min_max(node.left)
464
- isValid = isValid and node.val > l_max
465
- else :
466
- l_isValid, l_min = True , node.val
451
+ pre_val = float (' -inf' ) # result
452
+ WHITE , GRAY = 0 , 1
453
+ s = [(WHITE , root)]
467
454
468
- if node.right is not None :
469
- r_isValid, r_min, r_max = valid_min_max(node.right)
470
- isValid = isValid and node.val < r_min
455
+ while s:
456
+ color, node = s.pop()
457
+ if node is None : continue
458
+ if color == WHITE :
459
+ s.append((WHITE , node.right))
460
+ s.append((GRAY , node))
461
+ s.append((WHITE , node.left))
471
462
else :
472
- r_isValid, r_max = True , node.val
473
-
474
-
475
- return l_isValid and r_isValid and isValid, l_min, r_max
476
-
477
- return valid_min_max(root)[0 ]
478
- ```
479
-
480
- - 思路 3:利用二叉搜索树的性质,根结点为左子树的右边界,右子树的左边界,使用先序遍历自顶向下更新左右子树的边界并检查是否合法,迭代版本实现简单且可以提前返回结果。
481
-
482
- ``` Python
483
- class Solution :
484
- def isValidBST (self , root : TreeNode) -> bool :
485
-
486
- if root is None :
487
- return True
488
-
489
- s = [(root, float (' -inf' ), float (' inf' ))]
490
- while len (s) > 0 :
491
- node, low, up = s.pop()
492
- if node.left is not None :
493
- if node.left.val <= low or node.left.val >= node.val:
463
+ if node.val <= pre_val:
494
464
return False
495
- s.append((node.left, low, node.val))
496
- if node.right is not None :
497
- if node.right.val <= node.val or node.right.val >= up:
498
- return False
499
- s.append((node.right, node.val, up))
465
+ pre_val = node.val # result
466
+
500
467
return True
501
468
```
502
469
470
+
503
471
#### [ insert-into-a-binary-search-tree] ( https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/ )
504
472
505
473
> 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。
0 commit comments