shen464165094 发表于 2020-7-23 22:46:51

已知二叉树前序和中序遍历,求后序遍历

网上搜到的代码
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怎么来的??

shen464165094 发表于 2020-7-23 23:04:36

想起来了没有return就是return none

陈尚涵 发表于 2020-8-8 17:34:25

既然问题解决,那就设置个最佳答案吧~
页: [1]
查看完整版本: 已知二叉树前序和中序遍历,求后序遍历