-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_recreate_bt.py
48 lines (37 loc) · 1.23 KB
/
7_recreate_bt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
"""根据前序序列和中序序列重建二叉树
"""
def reConstructBinaryTree(self, pre, tin):
"""根据前序序列和中序序列重建二叉树
Args:
pre (list): 前序遍历序列
tin (list): 中序遍历序列
Returns:
TreeNode: 重建二叉树头
"""
if len(pre) != len(tin) or len(pre) == 0 or len(tin) == 0:
return None
root = TreeNode(pre[0])
if pre[0] in tin:
value = tin.index(pre[0])
else:
return None
left_tin, right_tin = tin[:value], tin[value+1:]
left_pre, right_pre = pre[1:1+len(left_tin)], pre[1+len(left_tin):]
root.left = self.reConstructBinaryTree(left_pre, left_tin)
root.right = self.reConstructBinaryTree(right_pre, right_tin)
return root
def main():
pre = [1, 2, 3, 4, 5, 6, 7]
tin = [3, 2, 4, 1, 6, 5, 7]
ex = Solution()
root = ex.reConstructBinaryTree(pre, tin)
print(root.val, ' ', root.left.val, ' ', root.right.val)
if __name__ == '__main__':
main()