# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def constructFromPrePost(self, pre, post): 
        if not pre: return None
        root = TreeNode(pre[0])
        pre = pre[1:]
        post = post[:-1]

        if not pre: return root

        t = pre[0]
        n = post.index(t)
        root.left = self.constructFromPrePost(pre[:n+1], post[:n+1])
        root.right = self.constructFromPrePost(pre[n+1:], post[n+1:])
        return root 

print(Solution().constructFromPrePost([1,2,4,5,3,6,7], [4,5,2,6,7,3,1]))
