77
88## 应用
99
10- [ validate-binary-search-tree] ( https://leetcode-cn.com/problems/validate-binary-search-tree/ )
10+ ### [ validate-binary-search-tree] ( https://leetcode-cn.com/problems/validate-binary-search-tree/ )
1111
1212> 验证二叉搜索树
1313
@@ -32,7 +32,7 @@ class Solution:
3232 return True
3333```
3434
35- [ insert-into-a-binary-search-tree] ( https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/ )
35+ ### [ insert-into-a-binary-search-tree] ( https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/ )
3636
3737> 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。
3838
@@ -51,7 +51,7 @@ class Solution:
5151 return root
5252```
5353
54- [ delete-node-in-a-bst] ( https://leetcode-cn.com/problems/delete-node-in-a-bst/ )
54+ ### [ delete-node-in-a-bst] ( https://leetcode-cn.com/problems/delete-node-in-a-bst/ )
5555
5656> 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
5757
@@ -97,7 +97,7 @@ class Solution:
9797 return dummy.left
9898```
9999
100- [ balanced-binary-tree] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
100+ ### [ balanced-binary-tree] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
101101
102102> 给定一个二叉树,判断它是否是高度平衡的二叉树。
103103
@@ -134,6 +134,40 @@ class Solution:
134134 return True
135135```
136136
137+ ### [ valid-bfs-of-bst] ( ./bst_bfs.py )
138+
139+ > 给定一个整数数组,求问此数组是不是一个 BST 的 BFS 顺序。
140+
141+ 此题是面试真题,但是没有在 leetcode 上找到原题。由于做法比较有趣也很有 BST 的特点,补充在这供参考。
142+
143+ ``` Python
144+ import collections
145+
146+ def bst_bfs (A ):
147+
148+ N = len (A)
149+ interval = collections.deque([(float (' -inf' ), A[0 ]), (A[0 ], float (' inf' ))])
150+
151+ for i in range (1 , N):
152+ while interval:
153+ lower, upper = interval.popleft()
154+ if lower < A[i] < upper:
155+ interval.append((lower, A[i]))
156+ interval.append((A[i], upper))
157+ break
158+
159+ if not interval:
160+ return False
161+
162+ return True
163+
164+ if __name__ == " __main__" :
165+ A = [10 , 8 , 11 , 1 , 9 , 0 , 5 , 3 , 6 , 4 , 12 ]
166+ print (bst_bfs(A))
167+ A = [10 , 8 , 11 , 1 , 9 , 0 , 5 , 3 , 6 , 4 , 7 ]
168+ print (bst_bfs(A))
169+ ```
170+
137171## 练习
138172
139173- [ ] [ validate-binary-search-tree] ( https://leetcode-cn.com/problems/validate-binary-search-tree/ )
0 commit comments