-
Notifications
You must be signed in to change notification settings - Fork 1
/
길 찾기 게임.py
79 lines (62 loc) · 1.87 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sys
# 재귀한도 늘려주기
sys.setrecursionlimit(10**6)
class Node:
def __init__(self, x, y, head):
self.x = x
self.y = y
self.head = head
self.left_child = None
self.right_child = None
class Tree:
def __init__(self, x, y, head):
self.root = Node(x, y, head)
def append_node(self, x, y, head):
cur_node = self.root
# 빈 공간 찾기
while True:
if x < cur_node.x:
if cur_node.left_child:
cur_node = cur_node.left_child
else:
cur_node.left_child = Node(x, y, head)
break
else:
if cur_node.right_child:
cur_node = cur_node.right_child
else:
cur_node.right_child = Node(x, y, head)
break
pre = []
post = []
# 재귀로 head -> left -> right 순서로 순회
def preorder(cur_node):
if cur_node:
pre.append(cur_node.head)
if cur_node.left_child:
preorder(cur_node.left_child)
if cur_node.right_child:
preorder(cur_node.right_child)
# 재귀로 left -> right -> head 순서로 순회
def postorder(cur_node):
if cur_node:
if cur_node.left_child:
postorder(cur_node.left_child)
if cur_node.right_child:
postorder(cur_node.right_child)
post.append(cur_node.head)
def solution(nodeinfo):
# 노드 번호 붙여주기
for idx, node in enumerate(nodeinfo):
nodeinfo[idx] = node + [idx + 1]
nodeinfo.sort(key=lambda x: -x[1])
# 트리 만들기
tree = Tree(*nodeinfo[0])
for idx, node in enumerate(nodeinfo):
if idx == 0:
continue
tree.append_node(*node)
# 순회
preorder(tree.root)
postorder(tree.root)
return [pre, post]