由前序、中序求后序

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]]

你可能感兴趣的:(由前序、中序求后序)