-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode889.py
More file actions
27 lines (24 loc) · 741 Bytes
/
Leetcode889.py
File metadata and controls
27 lines (24 loc) · 741 Bytes
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
class TreeNode:
def __init__(self,x):
self.val = x
self.left = None
self.right = None
class Solution:
def constructFromPrePost(self, pre, post):
"""
:type pre: List[int]
:type post: List[int]
:rtype: TreeNode
"""
if not pre: return
root = TreeNode(pre[0])
pre, post = pre[1:], post[:-1]
if not pre: return root
i = post.index(pre[0])
root.left = self.constructFromPrePost(pre[:i + 1], post[:i + 1])
root.right = self.constructFromPrePost(pre[i + 1:], post[i + 1:])
return root
test_pre = [1,2,4,5,3,6,7]
test_post = [4,5,2,6,7,3,1]
S = Solution()
x = S.constructFromPrePost(pre=test_pre,post=test_post)