106. Construct Binary Tree from Inorder and Postorder Traversal (Medium)

Description:

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

106. Construct Binary Tree from Inorder and Postorder Traversal (Medium)_第1张图片

Solution:

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
        if not inorder:
            return None
        
        root_val = postorder[-1]
        index = inorder.index(root_val)
        root = TreeNode(root_val)
        
        root.left = self.buildTree(inorder[:index],postorder[:index])
        root.right = self.buildTree(inorder[index+1:],postorder[index:len(postorder)-1])
        
        return root

Runtime: 216 ms, faster than 14.25% of Python3 online submissions for Construct Binary Tree from Inorder and Postorder Traversal.
Memory Usage: 87.7 MB, less than 28.74% of Python3 online submissions for Construct Binary Tree from Inorder and Postorder Traversal.

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
        if not inorder:
            return None
        
        i = -1
        j = -1
        last_head = -1
        node_hist = []
        while i > -len(inorder)-1 and j > -len(postorder)-1:
            index = postorder.index(inorder[i]) - len(postorder)
            if index <= j:
                cache = [TreeNode(postorder[k]) for k in range(j,index-1,-1)]
                for k in range(len(cache)-1):
                    cache[k].right = cache[k+1]
                if last_head > -1:
                    node_hist[last_head].left = cache[0]
                node_hist += cache
            last_head = -(index+1)
            i -= 1
            j = min(j,index-1)
            
        return node_hist[0]

Runtime: 108 ms, faster than 69.34% of Python3 online submissions for Construct Binary Tree from Inorder and Postorder Traversal.
Memory Usage: 14 MB, less than 99.24% of Python3 online submissions for Construct Binary Tree from Inorder and Postorder Traversal.

看来Python的正序比逆序要快

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
        if not inorder:
            return None
        
        inorder = inorder[::-1]
        postorder = postorder[::-1]
        
        i = 0
        j = 0
        last_head = -1
        node_hist = []
        while i < len(inorder) and j < len(postorder):
            index = postorder.index(inorder[i])
            if index >= j:
                cache = [TreeNode(postorder[k]) for k in range(j,index+1)]
                for k in range(index-j):
                    cache[k].right = cache[k+1]
                if last_head > -1:
                    node_hist[last_head].left = cache[0]
                node_hist += cache
            last_head = index
            i += 1
            j = max(j,last_head+1)
            
        return node_hist[0]

Runtime: 84 ms, faster than 73.42% of Python3 online submissions for Construct Binary Tree from Inorder and Postorder Traversal.
Memory Usage: 14 MB, less than 98.47% of Python3 online submissions for Construct Binary Tree from Inorder and Postorder Traversal.

你可能感兴趣的:(106. Construct Binary Tree from Inorder and Postorder Traversal (Medium))