from collections import deque
tree = [[],[2,3],[4,5],[6],[],[],[]]
def bfs(tree,root):
q = deque([root])
while q:
node = q.popleft()
print(node,end=' ')
for child in tree[node]:
q.append(child)
bfs(tree,1)
print()
def dfs(tree,node):
print(node,end=' ')
for child in tree[node]:
dfs(tree,child)
dfs(tree,1)
def tree_height(tree,node):
if not tree[node]:return 0
return 1+max(tree_height(tree, child) for child in tree[node] )
print(tree_height(tree, 1))
沒有留言:
張貼留言