일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- LeetCode
- 715. Range Module
- 운영체제
- 컴퓨터의 구조
- Generator
- Substring with Concatenation of All Words
- concurrency
- shiba
- kaggle
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- Python Code
- Class
- attribute
- 프로그래머스
- Decorator
- Python
- DWG
- 315. Count of Smaller Numbers After Self
- 밴픽
- 시바견
- iterator
- 30. Substring with Concatenation of All Words
- Regular Expression
- Protocol
- 파이썬
- Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- data science
- t1
Archives
- Today
- Total
Scribbling
LeetCode: 95. Unique Binary Search Trees II 본문
Computer Science/Coding Test
LeetCode: 95. Unique Binary Search Trees II
focalpoint 2021. 10. 13. 20:40# 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 generateTrees(self, n: int) -> List[Optional[TreeNode]]:
return self.tree_maker([i for i in range(1, n+1)])
def tree_maker(self, nums):
if not nums:
return [None]
ret = []
for i, num in enumerate(nums):
lefts = self.tree_maker(nums[:i])
rights = self.tree_maker(nums[i+1:])
temp = []
for c in range(len(lefts) * len(rights)):
temp.append(TreeNode(val=num))
c = 0
for l in range(len(lefts)):
for r in range(len(rights)):
temp[c].left = lefts[l]
temp[c].right = rights[r]
c += 1
ret += temp
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
프로그래머스: 다리를 지나는 트럭 (0) | 2021.10.15 |
---|---|
LeetCode: 96. Unique Binary Search Trees (0) | 2021.10.13 |
LeetCode: 92. Reverse Linked List II (0) | 2021.10.12 |
LeetCode: 97. Interleaving String (0) | 2021.10.11 |
LeetCode: 93. Restore IP Addresses (0) | 2021.10.11 |