7
7
8
8
## 应用
9
9
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/ )
11
11
12
12
> 验证二叉搜索树
13
13
@@ -32,7 +32,7 @@ class Solution:
32
32
return True
33
33
```
34
34
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/ )
36
36
37
37
> 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。
38
38
@@ -51,7 +51,7 @@ class Solution:
51
51
return root
52
52
```
53
53
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/ )
55
55
56
56
> 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
57
57
@@ -97,7 +97,7 @@ class Solution:
97
97
return dummy.left
98
98
```
99
99
100
- [ balanced-binary-tree] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
100
+ ### [ balanced-binary-tree] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
101
101
102
102
> 给定一个二叉树,判断它是否是高度平衡的二叉树。
103
103
@@ -134,6 +134,40 @@ class Solution:
134
134
return True
135
135
```
136
136
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
+
137
171
## 练习
138
172
139
173
- [ ] [ validate-binary-search-tree] ( https://leetcode-cn.com/problems/validate-binary-search-tree/ )
0 commit comments