최대 1 분 소요

Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
        if not inorder:
            return None
        
        current = TreeNode(preorder.pop(0))
        
        current.left = self.buildTree(preorder, inorder[:inorder.index(current.val)])
        current.right = self.buildTree(preorder, inorder[inorder.index(current.val) + 1:])

        return current

댓글남기기