Skip to content

Commit 16fc488

Browse files
committed
improved doc; added a problem to bst
1 parent 379f3c4 commit 16fc488

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

advanced_algorithm/backtrack.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Solution:
9494

9595
> 给定一个没有重复数字的序列,返回其所有可能的全排列。
9696
97-
思路 1:需要记录已经选择过的元素,满足条件的结果才进行返回,需要额外 O(n) 的空间
97+
- 思路 1:需要记录已经选择过的元素,满足条件的结果才进行返回,需要额外 O(n) 的空间
9898

9999
```Python
100100
class Solution:
@@ -125,7 +125,7 @@ class Solution:
125125
return result
126126
```
127127

128-
思路 2: 针对此题的更高级的回溯,利用原有的数组,每次回溯将新选择的元素与当前位置元素交换,回溯完成再换回来
128+
- 思路 2: 针对此题的更高级的回溯,利用原有的数组,每次回溯将新选择的元素与当前位置元素交换,回溯完成再换回来
129129

130130
```Python
131131
class Solution:

advanced_algorithm/binary_search_tree.md

+38-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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/)

advanced_algorithm/bst_bfs.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import collections
2+
3+
def bst_bfs(A):
4+
5+
N = len(A)
6+
interval = collections.deque([(float('-inf'), A[0]), (A[0], float('inf'))])
7+
8+
for i in range(1, N):
9+
while interval:
10+
lower, upper = interval.popleft()
11+
if lower < A[i] < upper:
12+
interval.append((lower, A[i]))
13+
interval.append((A[i], upper))
14+
break
15+
16+
if not interval:
17+
return False
18+
19+
return True
20+
21+
if __name__ == "__main__":
22+
A = [10, 8, 11, 1, 9, 0, 5, 3, 6, 4, 12]
23+
print(bst_bfs(A))
24+
A = [10, 8, 11, 1, 9, 0, 5, 3, 6, 4, 7]
25+
print(bst_bfs(A))

0 commit comments

Comments
 (0)