7. 重建二叉树(buildTree)
1. python
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
if not preorder and not inorder:
return None
root =TreeNode(preorder[0])
inorderIndex=inorder.index(preorder[0])
root.left = self.buildTree(preorder[1:inorderIndex+1],inorder[:inorderIndex])#1---index+,0---index
root.right= self.buildTree(preorder[inorderIndex+1:],inorder[inorderIndex+1:])#index+1---,index+1----
return root