LeetCode/공통 11

797. All Paths From Source to Target

797. All Paths From Source to Target 이 문제는 DAG(Directed Acyclic Graph, 방향 비순환 그래프)에서 시작 노드(0)에서 목표 노드(n−1)로 가는 모든 경로를 찾는 것입니다. 이 과정은 DFS(깊이 우선 탐색)를 이용하여 해결할 수 있습니다. 아래는 코드의 주요 흐름에 대한 한글 설명입니다.DFS 함수 정의:dfs 함수는 현재 노드(node)와 현재까지 탐색한 경로(path)를 인자로 받습니다.만약 현재 노드가 목표 노드(n−1)라면, 지금까지의 경로를 결과 리스트(result)에 추가합니다.재귀와 백트래킹:현재 노드의 모든 이웃 노드(neighbor)를 탐색합니다.이웃 노드를 경로에 추가한 뒤, 재귀적으로 dfs를 호출합니다.재귀 호출이 끝난 후에는 ..

LeetCode/공통 2024.12.27

[LeetCode 75] Medium - 236. Lowest Common Ancestor of a Binary Tree

236. Lowest Common Ancestor of a Binary Tree LCA(최저 공통 조상) 찾기노드 자신도 공통조상이 될수 있음재귀로..일단 루트가 none 이면 return 또는 return root루트가 p 또는 q 인지 체크해서 맞으면 루트 반환아니면, ..class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': if not root: # 빈 노드면 return ## return root if root == p or root == q: return root # 좌측에..

LeetCode/공통 2024.11.17

[LeetCode 75] Medium - 452. Minimum Number of Arrows to Burst Balloons

452. Minimum Number of Arrows to Burst Balloons There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.Arrows can be shot up directly vertic..

LeetCode/공통 2024.11.10