Skip to content

Commit a72cb05

Browse files
committed
update binary search & tree
1 parent 8e939c4 commit a72cb05

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

binary_search.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,30 @@ if __name__ == "__main__":
107107
seq = [32, 55, 54, 54, 54, 54, 32, 15, 6, 4, 2, 1]
108108
print(search_max_num(seq, 0, len(seq)))
109109
```
110+
### 二维数组的查找
111+
class Solution:
112+
# array 二维列表
113+
114+
def find(self, target, array):
115+
# write code here
116+
for arr in array:
117+
lft, rgt =0, len(arr) - 1
118+
while lft <= rgt:
119+
mid = (lft + rgt) // 2
120+
if target > arr[mid]:
121+
lft = mid + 1
122+
elif target < arr[mid]:
123+
rgt = mid - 1
124+
else:
125+
return arr[mid]
126+
return 'No target'
127+
128+
129+
target = 8
130+
array = [
131+
[1, 3, 5, 7, 9],
132+
[2, 4, 6, 8, 10]
133+
]
134+
solution = Solution()
135+
solution.find(target, array)
136+
```

binary_tree.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,69 @@ def pre_in_post(pre_order, in_order):
239239
post.right = pre_in_post(pre_order[index+1:], in_order[index+1:])
240240
return post
241241
```
242+
### 已知前序中序构造出树
243+
```python
244+
# -*- coding:utf-8 -*-
245+
class TreeNode:
246+
def __init__(self, x):
247+
self.val = x
248+
self.left = None
249+
self.right = None
250+
251+
class Solution:
252+
# 返回构造的TreeNode根节点
253+
def reConstructBinaryTree(self, pre, tin):
254+
# write code here
255+
if not pre:
256+
return
257+
tree = TreeNode(pre[0])
258+
index = tin.index(pre[0])
259+
tree.left = self.reConstructBinaryTree(pre[1:index+1],tin[:index])
260+
tree.right = self.reConstructBinaryTree(pre[index+1:],tin[index+1:])
261+
return tree
262+
263+
@classmethod
264+
def print_tree(cls, tree):
265+
if tree:
266+
cls.print_tree(tree.left)
267+
cls.print_tree(tree.right)
268+
print(tree.val)
269+
270+
pre = [1,2,3,4,5,6,7]
271+
tin = [3,2,4,1,6,5,7]
272+
s = Solution()
273+
t = s.reConstructBinaryTree(pre, tin)
274+
s.print_tree(t)
275+
```
276+
树的子结构
277+
278+
```python
279+
# -*- coding:utf-8 -*-
280+
# class TreeNode:
281+
# def __init__(self, x):
282+
# self.val = x
283+
# self.left = None
284+
# self.right = None
285+
class Solution:
286+
def is_subtree(self, t1, t2):
287+
if not t2:
288+
return True
289+
if not t1:
290+
return False
291+
if not t1.val == t2.val:
292+
return False
293+
return self.is_subtree(t1.left, t2.left) and self.is_subtree(t1.right, t2.right)
294+
295+
def HasSubtree(self, pRoot1, pRoot2):
296+
# write code here
297+
result = False
298+
if pRoot1 and pRoot2:
299+
if pRoot1.val == pRoot2.val:
300+
result = self.is_subtree(pRoot1, pRoot2)
301+
if not result:
302+
result = self.is_subtree(pRoot1.left, pRoot2)
303+
if not result:
304+
result = self.is_subtree(pRoot1.right, pRoot2)
305+
return result
306+
```
307+

0 commit comments

Comments
 (0)