In [39]: class TreeNode:
...: def __init__(self, x):
...: self.val = x
...: self.left = None
...: self.right = None
In [50]:
...:
...:
...:
...:
...:
...:
...: class Solution:
...: def buildTree(self, preorder, inorder):
...: """
...: :type preorder: List[int]
...: :type inorder: List[int]
...: :rtype: TreeNode
...: """
...: if inorder:
...: idx = inorder.index(preorder.pop(0))
...: root = TreeNode(inorder[idx])
...: root.left = self.buildTree(preorder, inorder[0:idx])
...: root.right = self.buildTree(preorder, inorder[idx+1:])
...: return root
In [52]: pre_o = [3,9,20,15,7]
In [53]: in_o = [9,3,15,20,7]
In [54]: s = Solution(); t = s.buildTree(pre_o, in_o); print(t)
<__main__.TreeNode object at 0x10d8128d0>
In [55]: t.right.val
Out[55]: 20
In [56]: t.right.left.val
Out[56]: 15
In [57]: t.right.right.val
Out[57]: 7