-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path트리 순회.py
53 lines (46 loc) · 1.25 KB
/
트리 순회.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 이진 탐색 트리 구현
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):
bst = BST()
for key in lst:
bst.insert(key)
result = []
for search_val in search_lst:
if bst.search(search_val):
result.append(True)
else:
result.append(False)
return result