寻找二叉树的所有路径

给定一棵二叉树,返回该树的所有的路径。 

class Node(object):

    def __init__(self, value):

        self.value = value

        self.left_node =None

        self.right_node =None

def find_path(root, pathes):

"""

    返回所有的路径

    :param root: 当前节点

    :param pathes: 当前节点包含的路径

    :return:

"""

    if not root:

        return pathes

    else:

        pathes =set(path +str(root.value)for pathin pathes)

        left_pathes = find_path(root.left_node, pathes)

        right_pathes = find_path(root.right_node, pathes)

        all_pathes = left_pathes | right_pathes

    return all_pathes

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容