BFS
#BFS 队列实现
from collections import deque
#也可引用Queue from queue
def bfs(root):
result = []
q = deque()
q.append(root)
while q:
x = q.popleft()
result.append(x)
for i in Tree[x]:
q.append(i)
return result
DFS
#DFS 递归实现
#树是单向边
def dfs(u):
print(u)
for v in Tree[u]:
dfs(v)
#双向边
def dfs(u,fa):
print(u)
for v in Tree[u]:
if v== fa:
continue
dfs(v,u)