栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

枚举树中的所有路径

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

枚举树中的所有路径

每当您在树上发现问题时,只需使用递归:D

def paths(tree):  #Helper function  #receives a tree and   #returns all paths that have this node as root and all other paths  if tree is the empty tree:    return ([], [])  else: #tree is a node    root = tree.value    rooted_paths = [[root]]    unrooted_paths = []    for subtree in tree.children:        (useable, unueseable) = paths(subtree)        for path in useable: unrooted_paths.append(path) rooted_paths.append([root]+path)        for path in unuseable: unrooted_paths.append(path)    return (rooted_paths, unrooted_paths)def the_function_you_use_in_the_end(tree):   a,b = paths(tree)   return a+b


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637441.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号