-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstruct-binary-tree-from-preorder-and-postorder-traversal.py
51 lines (47 loc) · 2.12 KB
/
construct-binary-tree-from-preorder-and-postorder-traversal.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
49
50
51
class Solution(object):
def constructFromPrePost(self, preorder, postorder):
"""
:type preorder: List[int]
:type postorder: List[int]
:rtype: TreeNode
"""
def build(preorder, prestart, preend, postorder, poststart, postend):
if prestart > preend:
return None
if prestart == preend:
return TreeNode(preorder[prestart]) # 我的问题是少了这一步
root_val = preorder[prestart]
left_postend = preorder[prestart+1]
index = postorder.index(left_postend)
leftsize = index-poststart+1
print(prestart+leftsize)
root = TreeNode(root_val)
root.left = build(preorder, prestart+1, prestart +
leftsize, postorder, poststart, index)
root.right = build(preorder, prestart+leftsize+1,
preend, postorder, index+1, postend-1)
return root
return build(preorder, 0, len(preorder)-1, postorder, 0, len(postorder)-1)
####myun
class my_solution(object):
def constructFromPrePost(self, preorder, postorder):
"""
:type preorder: List[int]
:type postorder: List[int]
:rtype: TreeNode
"""
def build (preorder,prestart,preend,postorder,poststart,postend):
if prestart>preend: return None
if prestart==preend:return TreeNode(preorder[prestart])
root_val=preorder[prestart]
left_postend=preorder[prestart+1]
print(left_postend)
right_prestart=postorder[postend-1]
print(right_prestart)
left_postend_index=postorder.index(left_postend)
right_prestart_index=preorder.index(right_prestart)
root=TreeNode(root_val)
root.left=build(preorder,prestart+1,right_prestart_index-1,postorder,poststart,left_postend_index)
root.right=build(preorder,right_prestart_index,preend,postorder,left_postend_index+1,postend-1)
return root
return build (preorder,0,len(preorder)-1,postorder,0,len(postorder)-1)