错误在34行(看你的最下面的提示,那个文件才是你自己编写的)
上面那些,都是你的代码又调用了系统库,在系统库里产生错误的行,和你关系不大。
代码贴出来看看
不知道你这个问题是否已经解决, 如果还没有解决的话:# Judge the same binary search tree
# Edited by Ocean_Waver
# 由于二叉搜索树不同于二叉树,仅根据二叉搜索树的先序遍历结果就能判定二叉树的唯一形状;
# 因此我们可以通过比较两个二叉搜索树的先序遍历结果来判断他们是否为同一个二叉搜索树;
class node:
def __init__(self, elem=None, lchild=None, rchild=None):
self.elem = elem
self.lchild = lchild
self.rchild = rchild
class tree:
def __init__(self, root=node()):
self.root = root
def bi_search_tree_establish(List): # 根据输入的列表建立二叉搜索树
if List:
mytree = tree(node(List[0]))
for i in range(1, len(List)):
temp_node = mytree.root
while temp_node:
if List[i] < temp_node.elem:
if temp_node.lchild:
temp_node = temp_node.lchild
else:
temp_node.lchild = node(List[i])
break
else:
if temp_node.rchild:
temp_node = temp_node.rchild
else:
temp_node.rchild = node(List[i])
break
return mytree
else:
return None
def preorder_probing(now_node, pre_L): # 先序遍历——递归实现
# print(now_node.elem)
pre_L.append(now_node.elem)
if now_node.lchild:
preorder_probing(now_node.lchild, pre_L)
if now_node.rchild:
preorder_probing(now_node.rchild, pre_L)
def cmp(a, b): # 比较序列函数,Python3的常用函数库里已经没有cmp函数了
leng = len(a)
if leng == len(b):
for i in range(0, leng):
if a[i] != b[i]:
# print("False")
return -1
# print("True")
return 0
else:
# print("False")
return -1
if __name__ == "__main__":
N = int(input()) # 输入n表示需要检测的组数
S_List = [int(i) for i in input()] # 输入一个二叉搜索树序列作为标准,与下面的进行比较
S_Tree = bi_search_tree_establish(S_List) # 构建标准二叉搜索树
if S_Tree:
S_pre_list = []
preorder_probing(S_Tree.root, S_pre_list)
for i in range(0, N):
List = [int(i) for i in input()] # 输入待比较的二叉搜索树序列
MyTree = bi_search_tree_establish(List) # 构建待比较二叉搜索树
if MyTree:
pre_list = []
preorder_probing(MyTree.root, pre_list)
if cmp(S_pre_list, pre_list) == 0:
print("YES")
else:
print("NO")