← 返回 bytedance 的题目列表Bidirectional Graph Traversal
类型:online_judge
Given an undirected graph, write a function to traverse the graph using both Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms and return the order of traversal.
Input Format:
The first line contains an integer n, the number of nodes.
The following n lines describe edges between nodes with two integers u and v.
Output Format:
First line: BFS traversal order.
Second line: DFS traversal order.
Test Cases:
Input:
5
0 1
0 2
1 3
1 4
Output:
BFS order: 0 -> 1 -> 2 -> 3 -> 4
DFS order: 0 -> 1 -> 3 -> 4 -> 2
Example
Input
5
0 1
0 2
1 3
1 4