Zero to Hero
article thumbnail
1305. All Elements in Two Binary Search Trees
Algorithm 2021. 8. 21. 11:14

All Elements in Two Binary Search Trees - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 2개의 이진 탐색 트리가 주어진다. 두 트리의 value값을 오름차순 정렬한 list를 반환하는 문제다. Input: root1 = [2,1,4], root2 = [1,0,3] Output: [0,1,1,2,3,4] 1. 중위 순회(in-order traversal) class Solution: def getAllElements(self, r..

article thumbnail
894. All Possible Full Binary Trees
Algorithm 2021. 8. 20. 13:57

All Possible Full Binary Trees - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 트리를 구성하는 노드 개수가 정수로 주어진다. 주어진 노드 개수를 전부 소비해서 만들 수 있는 가능한 모든 포화 이진트리(full-binary tree)를 반환하는 문제다. 물론 중복은 제외한다. 여기서 말하는 중복이란 동일한 모양을 가진 포화 이진트리를 말한다. 여기서 포인트는 가짓수를 반환하는 게 아니다. 실제로 트리를 반환해야 한다. 위의 예시 같은 경..

article thumbnail
1605. Find Valid Matrix Given Row and Column Sums
Algorithm 2021. 8. 18. 22:00

Find Valid Matrix Given Row and Column Sums - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 0 이상의 정수로 구성된 임의의 2차원 배열이 있다고 가정하자. n번째 row의 합 정보를 담고 있는 rowSum과 n번째 col의 합 정보를 담고 있는 colSum이 주어진다. rowSum과 colSum을 이용해 이 조건에 해당하는 2차원 배열을 반환하는 문제다. 정답은 여러 개가 나올 수 있고 그중 하나만 반환하면 된다. 예시 설..

article thumbnail
1008. Construct Binary Search Tree from Preorder Traversal
Algorithm 2021. 8. 16. 16:57

Construct Binary Search Tree from Preorder Traversal - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 이진 탐색 트리의 전위 순회 결과가 list로 주어진다. 이를 이용해 이진 탐색 트리를 복원하는 문제다. 1. DFS from typing import * from collections import deque class Solution: def bstFromPreorder(self, preorder: List[int])..

article thumbnail
2021번: 최소 환승 경로
Algorithm 2021. 8. 15. 17:16

2021번: 최소 환승 경로 첫째 줄에 역의 개수 N(1≤N≤100,000), 노선의 개수 L(1≤L≤100,000)이 주어진다. 다음 L개의 줄에는 각 노선이 지나는 역이 순서대로 주어지며 각 줄의 마지막에는 -1이 주어진다. 마지막 줄에는 출발 www.acmicpc.net import sys from collections import deque, defaultdict r = sys.stdin.readline node_num, flag_num = map(int, r().split()) graph = defaultdict(set) for i in range(1, flag_num + 1): temp = list(map(int, r().split())) temp.pop() for node in temp: g..

article thumbnail
797. All Paths From Source to Target
Algorithm 2021. 8. 15. 15:14

All Paths From Source to Target - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 그래프의 edge 정보가 주어진다. 0번 노드 ~ N-1번 노드까지 가능한 모든 경로를 구해 반환하는 문제다. 1. DFS from typing import * class Solution: def allPathsSourceTarget(self, input_list: List[List[int]]) -> List[List[int]]: graph = {} len..