剑指offer,page62
题目:根据前序和中序,重建二叉树
分析:树的问题用递归去做,重点在于构造最小结构。对前序,第一个是root,剩下是左子树和右子树,对中序,是左子树-root-右子树,因此可以抓住root
levelorder = []
def rebuildTree(preorder,inorder):
for x in range(len(inorder)):
if inorder[x].value==preorder[0].value:
# find the root
levelorder.append(inorder[x])
rebuildTree(inorder[:x])
rebuildTree(inorder[x+1:])
break