0%
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: if inorder==[]: return None root = TreeNode(postorder[-1]) if len(inorder)==1: return root
root_value = postorder[-1] for index in range(0,len(inorder)): if inorder[index]==root_value: break left_inorder,right_inorder = inorder[:index],inorder[index+1:] left_postorder,right_postorder = postorder[:index],postorder[index:-1] root.left = self.buildTree(left_inorder,left_postorder) root.right = self.buildTree(right_inorder,right_postorder) return root
|