[7급 국가직 알고리즘 2026년 25번]

다음 파이썬 프로그램의 실행 결과는?
class NodeBS(object):
	def __init__(self, vl = None):
		self.vl = vl
		self.root = None
		self.lf = None
		self.rg = None
	def _ad(self, vl):
		nd = NodeBS(vl)
		if vl > self.vl:
			self.rg = self.rg and self.rg._ad(vl) or nd
		elif vl < self.vl:
			self.lf = self.lf and self.lf._ad(vl) or nd
		else:
			print("node duplicated")
		return self
class Bst(NodeBS):
	def add_node(self, vl):
		if not self.root:
			self.root = NodeBS(vl)
		else:
			self.root._ad(vl)
	def order(self):
		current = self.root
		nodes, stack = [], []
		while stack or current:
			if current:
				nodes.append(current.vl)
				stack.append(current)
				current = current.lf
			else:
				current = stack.pop()
				current = current.rg
		return nodes
if __name__ == "__main__":
	bt = Bst()
	in_list = [6, 4, 8, 2, 5, 7, 9, 1, 3, 10]
	for i in in_list:
		bt.add_node(i)
	print(bt.order())
알고리즘
  1. ①[6, 4, 2, 1, 3, 8, 5, 7, 9, 10]
  2. ②[6, 4, 2, 1, 5, 8, 3, 7, 9, 10]
  3. ③[6, 4, 2, 1, 3, 5, 8, 7, 9, 10]
  4. ④[6, 4, 2, 1, 5, 3, 8, 7, 9, 10]

선지를 골라 바로 채점해 보세요.

정답 ③

출처: 인사혁신처 공개 기출(공공데이터포털, 이용허락 제한 없음)

신고가 접수됐습니다. 감사합니다.

오류 신고