일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- DWG
- kaggle
- Convert Sorted List to Binary Search Tree
- 프로그래머스
- 밴픽
- data science
- Python Implementation
- 30. Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- t1
- concurrency
- iterator
- shiba
- Protocol
- Python
- Python Code
- 시바견
- 43. Multiply Strings
- Regular Expression
- Decorator
- 109. Convert Sorted List to Binary Search Tree
- 파이썬
- 715. Range Module
- Generator
- 컴퓨터의 구조
- LeetCode
- attribute
- 운영체제
- Class
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 |