← 返回 apple 的题目列表Binary Tree Traversals: BFS and DFS
类型:online_judge
Given a binary tree, write code to implement the following:
Perform Breadth-First Search (BFS) traversal of the binary tree and output the order of visited nodes.
Perform Depth-First Search (DFS) traversal recursively and output the order of visited nodes.
Perform Depth-First Search (DFS) iteratively and output the order of visited nodes.
Provide a test case:
Input: Root node of a binary tree, as below:
1
/ \
2 3
/ \ / \
4 5 6 7
Output:
BFS order: [1, 2, 3, 4, 5, 6, 7]
Recursive DFS order: [1, 2, 4, 5, 3, 6, 7]
Iterative DFS order: [1, 2, 4, 5, 3, 6, 7]
Example
Input
1,2,3,4,5,6,7
->
Structure:
1
/ \
2 3
/ \ / \
4 5 6 7