프로그래머스 - 길 찾기 게임
문제 보기
import sys
sys.setrecursionlimit(10 ** 7)
class Tree:
def __init__(self, datas):
self.root = max(datas, key=lambda x: x[1])
self.index = self.root[2]
_left = list(filter(lambda x: x[0] < self.root[0], datas))
_right = list(filter(lambda x: x[0] > self.root[0], datas))
if _left == []:
self.left = None
else:
self.left = Tree(_left)
if _right == []:
self.right = None
else:
self.right = Tree(_right)
def go(tree, post, pre):
post.append(tree.index)
print(tree.root)
if tree.left:
go(tree.left, post, pre)
if tree.right:
go(tree.right, post, pre)
pre.append(tree.index)
def solution(nodeinfo):
for i in range(len(nodeinfo)):
nodeinfo[i].append(i + 1)
post = []
pre = []
tree = Tree(nodeinfo)
go(tree, post, pre)
return [post, pre]