Construct Binary Tree from Inorder and Postorder Traversal --- 8 lines in python


  • 3
    W
    def buildTree2(self,inorder,postorder):
        if len(inorder) ==0 or len(postorder) ==0:
            return None
        curr = postorder.pop(-1)
        root = TreeNode(curr)
        mid = inorder.index(curr)
        root.right = self.buildTree2(inorder[mid+1:],postorder)
        root.left = self.buildTree2(inorder[:mid],postorder)
        return root

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.