1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree. ### Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
7 2 3 1 5 7 6 4 1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
这道题干啥呢
两行数字分别是二叉树后序和中序遍历的结果,要求你输出这个二叉树层次遍历(BFS)的结果。
算法
很久没写二叉树的代码了思路混乱写了一大堆,然后看到了一个看起来很萌的小姐姐的博客,写的好简单,比其他人写了一大堆相比真是赏心悦目!学习学习! 主要为利用中序和后序的结果,进行前序遍历,记录这个节点在满二叉树中的位置。最后把结果利用这个位置排序,直接就是层次遍历的结果啦,不需要建树,也不需要BFS,很简单。
代码
1 |
|