美文网首页
由前序、中序求后序

由前序、中序求后序

作者: cookyo | 来源:发表于2019-07-23 16:40 被阅读0次
def dfs(pre, tin):
    if len(pre)==1:
        return [pre[0]]
    if len(pre) == 0:
        return []
    root = pre[0]
    root_index = tin.index(pre[0])
    res_left = dfs(pre[1: root_index+1], tin[: root_index])
    res_right = dfs(pre[root_index+1: ], tin[root_index+1:])
    return res_left+res_right+[pre[0]]

相关文章

网友评论

      本文标题:由前序、中序求后序

      本文链接:https://www.haomeiwen.com/subject/sdjalctx.html