已知二叉树前序和中序遍历,求后序遍历
网上搜到的代码pre =
tin =
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
aftree.append(pre)
print(aftree,'0',sep=' ')
i = 0
for i in range(len(tin)):
if tin == pre:
break
left_pre = pre
right_pre = pre
left_tin = tin[:i]
right_tin = tin
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中,这两个none怎么来的?? 想起来了没有return就是return none 既然问题解决,那就设置个最佳答案吧~
页:
[1]