일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 715. Range Module
- Decorator
- 시바견
- Python Code
- 30. Substring with Concatenation of All Words
- iterator
- Protocol
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- Convert Sorted List to Binary Search Tree
- shiba
- Class
- 컴퓨터의 구조
- t1
- 파이썬
- Generator
- Python
- attribute
- 밴픽
- data science
- 315. Count of Smaller Numbers After Self
- Substring with Concatenation of All Words
- concurrency
- 43. Multiply Strings
- kaggle
- Regular Expression
- 운영체제
- 프로그래머스
- DWG
Archives
- Today
- Total
Scribbling
LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal 본문
Computer Science/Coding Test
LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal
focalpoint 2021. 10. 20. 16:42재귀로 풀어주자
# 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]:
return self.builder(preorder, inorder)
def builder(self, preorder, inorder):
if not preorder:
return None
head = TreeNode(val=preorder[0])
boundary_idx = inorder.index(preorder[0])
left_preorder = preorder[1:boundary_idx+1]
left_inorder = inorder[:boundary_idx]
right_preorder = preorder[boundary_idx+1:]
right_inorder = inorder[boundary_idx+1:]
head.left = self.builder(left_preorder, left_inorder)
head.right = self.builder(right_preorder, right_inorder)
return head
'Computer Science > Coding Test' 카테고리의 다른 글
프로그래머스: 구명보트 (0) | 2021.10.21 |
---|---|
프로그래머스: 큰 수 만들기 (0) | 2021.10.20 |
LeetCode: 103. Binary Tree Zigzag Level Order Traversal (0) | 2021.10.20 |
LeetCode: 102. Binary Tree Level Order Traversal (0) | 2021.10.20 |
프로그래머스: 모의고사 (0) | 2021.10.20 |