Problem Solving/LeetCode

2023.03.16 Today's Challenge

fabichoi 2023. 3. 16. 23:45

https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/

 

Construct Binary Tree from Inorder and Postorder Traversal - LeetCode

Can you solve this real interview question? Construct Binary Tree from Inorder and Postorder Traversal - Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the

leetcode.com

트리 순회 문제

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        if not inorder:
            return None

        root_val = postorder.pop()
        root = TreeNode(root_val)

        inorder_index = inorder.index(root_val)

        root.right = self.buildTree(inorder[inorder_index + 1:], postorder)
        root.left = self.buildTree(inorder[:inorder_index], postorder)

        return root

 

반응형