백트래킹은
기본적으로 Tree..
DFS의 Preorder Traversal (전위 순회: Root → Left → Right)로 풀이 가능
https://hyunkookim.tistory.com/365
BST 이진 검색 트리(height, Depth, DFS 검색 방법: inorder, preorder, postorder)
height 는 자식이 없는 노드가 1이고Depth 는 부모 root 노드가 1로 시작 DFS 검색 방법: 깊이 우선 탐색 (Depth-First Search, DFS)깊이 우선 탐색(DFS)은 코딩 인터뷰에서 가장 자주 등장하는 알고리즘 중 하
hyunkookim.tistory.com
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def canReachLeaf(root):
if not root or root.val == 0:
return False
if not root.left and not root.right:
return True
if canReachLeaf(root.left):
return True
if canReachLeaf(root.right):
return True
return False
def leafPath(root, path):
if not root or root.val == 0:
return False
path.append(root.val)
if not root.left and not root.right:
return True
if leafPath(root.left, path):
return True
if leafPath(root.right, path):
return True
path.pop()
return False
참고 강의
https://youtu.be/Ar40zcPoKEI?si=ANrQRht6S-ERkYOl
https://youtu.be/atTzqxbt4DM?si=OTMYb-8eRMltSgTO
https://www.geeksforgeeks.org/find-paths-given-source-destination/
Print all paths from a given source to a destination - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
'Coding Test > 알고리즘 이론' 카테고리의 다른 글
코딩 테스트 이론 - 그리디(greedy) 탐욕 알고리즘 (0) | 2024.11.11 |
---|---|
코딩 테스트 이론 - 단조 스택(Monotonic Stack) (0) | 2024.11.10 |
[LeetCode] Leetcode 75 Questions (NeetCode on yt) (5) | 2024.11.10 |
코딩 테스트 이론 - 브루트포스(Brute Force) (0) | 2024.11.09 |
코딩 테스트 이론 - 다이나믹 프로그래밍(DP) (1) | 2024.11.08 |