Skip to content

Commit

Permalink
26
Browse files Browse the repository at this point in the history
  • Loading branch information
park-jiyeong committed Jul 15, 2024
1 parent b8956e8 commit 64ae360
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions JYPARK/21to30/27.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

class BST:
def __init__(self):
self.root = None
def insert(self, key):
#루트 노드가 없는 경우 새로운 노드를 루트 노드로 추가
if not self.root:
self.root = Node(key)
else:
curr = self.root
while True:
if key < curr.val:
if curr.left:
curr = curr.left
else:
curr.left = Node(key)
break
else:
if curr.right:
curr = curr.right
else:
curr.right = Node(key)
break
def search(self, key):
curr = self.root
while curr and curr.val != key:
if key < curr.val:
curr = curr.left
else:
curr = curr.right
return curr


def solution(lst, search_lst):
answer = []
bst = BST()
for node in lst:
bst.insert(node)

for key in search_lst:
result = bst.search(key) #Node객체가 반환된다
if result != None and result.val == key:
answer.append(True)
else:
answer.append(False)
return answer

lst = [5, 3, 8, 4, 2, 1, 7, 10]
search_lst = [1, 2, 5, 6]
#solution(lst, search_lst)
print(solution(lst, search_lst))

0 comments on commit 64ae360

Please sign in to comment.