How can i make a FindMaxDepth method / This function will find the longest depth of the tree (the distance between the root node and the last leaf node), the input that the function will take is: (graph, rootNode) and the output will be a numeric value.
Example: If the following graph is entered and the startNode = A, the output must be 4
graph = {
"A": ["B", "C"],
"B": ["D", "E"],
"C": ["F"],
"D": [],
"E": ["F"],
"F": [],
}
visited = []
def dfs(visited, graph, root):
if root not in visited:
visited.append(root)
print(root)
for child in graph[root]:
dfs(visited, graph, child)
dfs(visited, graph, "A")
def bfs(graph, root):
visited = []
queue = []
visited.append(root)
queue.append(root)
while queue:
x = queue.pop(0)
print(x)
for child in graph[x]:
if child not in visited:
visited.append(child)
queue.append(child)
# bfs(graph, "A")