Why does my solution not work?


  • 0
    I

    I can't figure out why my solution gives the wrong answer, here is hte code:

    class Solution(object):
        def buildTreeHelper(self, inorder, postorder, index_map):
            if not inorder or not postorder:
                return None
                
            root = TreeNode(postorder.pop())
            root_idx = index_map[root.val]
            
            root.right = self.buildTreeHelper(inorder[root_idx + 1:], postorder, index_map)
            root.left = self.buildTreeHelper(inorder[:root_idx], postorder, index_map)
            
            return root
            
        
        def buildTree(self, inorder, postorder):
            """
            :type inorder: List[int]
            :type postorder: List[int]
            :rtype: TreeNode
            """
            index_map = {inorder[i]:i for i in xrange(len(inorder))}
            return self.buildTreeHelper(inorder, postorder, index_map)

Log in to reply
 

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