寻找二叉树的所有路径

2021-10-14  本文已影响0人  612twilight

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

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

上一篇下一篇

猜你喜欢

热点阅读