import sys
class TreeNode():
def __init__(self, data=-1):
self.data = data
self.leftChild, self.rightChild = None, None
def insert(self, data):
prior = self
while True:
if prior.data >= data:
if prior.leftChild == None:
prior.leftChild = TreeNode(data)
print(prior.data)
break
prior = prior.leftChild
else:
if prior.rightChild == None:
prior.rightChild = TreeNode(data)
print(prior.data)
break
prior = prior.rightChild
newCase = True
for line in sys.stdin:
line = line.strip()
if newCase:
N = int(line)
newCase = False
else:
newCase = True
nums = list(map(int,line.split()))
root = TreeNode()
for num in nums:
root.insert(num)
KY207 二叉排序树
最新推荐文章于 2024-08-19 11:12:00 发布