파비의 매일매일 공부기록

2023.03.16 Today's Challenge 본문

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

 

반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.03.18 Today's Challenge  (0) 2023.03.18
2023.03.17 Today's Challenge  (0) 2023.03.17
2023.03.15 Today's Challenge  (0) 2023.03.15
2023.03.14 Today's Challenge  (0) 2023.03.14
2023.03.13 Today's Challenge  (0) 2023.03.13
Comments