|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
网上搜到的代码
pre = [3, 9, 20, 15, -6, 7]
tin = [9, 3, -6, 15, 20, 7]
global aftree
global ai
aftree = []
ai = 0
def reConstruction(pre,tin):
print(pre,tin,sep=' ')
if len(pre) == 0:
return 'a'
if len(pre) == 1:
return pre[0]
aftree.append(pre[0])
print(aftree,'0',sep=' ')
i = 0
for i in range(len(tin)):
if tin[i] == pre[0]:
break
left_pre = pre[1:1+i]
right_pre = pre[1+i:]
left_tin = tin[:i]
right_tin = tin[i+1:]
aftree.append(reConstruction(right_pre,right_tin))
print(aftree,'r',sep=' ')
aftree.append(reConstruction(left_pre,left_tin))
print(aftree,'l',sep=' ')
print('ai')
reConstruction(pre,tin)
tmp = []
for i in aftree:
if i != None:
tmp.append(i)
#res = ''.join(str(tmp))
print(tmp[::-1])
实在搞不懂,aftree中[3, 20, 7, 15, 'a', -6, None, None, 9],这两个none怎么来的?? |
|