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
Code
传统做法:建立二叉树
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef struct node
{
node *left;
node *right;
int val;
node(int x)
{
left = NULL;
right = NULL;
val = x;
}
} node;
node *build_tree(int *post, int *in, int len)
{
if (len == 0)
return NULL;
int index;
// int len_post = sizeof(post)/sizeof(post[0]);
node *root = new node(post[len - 1]); // 每个子树的根结点
for (int i = 0; i < len; i++)
{
if (in[i] == root->val)
{
index = i;
break;
}
}
// 在中序序列的根结点的左边找左子树
root->left = build_tree(post, in, index);
// 在中序序列的根结点的右边找右子树
// 后序序列要index,因为index个节点不会是右子树的
root->right = build_tree(post + index, in + index + 1, len - index - 1);
return root;
// 7
// 2 3 1 5 7 6 4 后序
// 1 2 3 4 5 6 7 中序
}
void pre_visit(node *root)
{
if (root == nullptr)
return;
cout << root->val << " ";
pre_visit(root->left);
pre_visit(root->right);
return;
}
void eachfloor_visit(node *root)
{
if (root == nullptr)
return;
queue<node *> q;
cout << root->val;
if (root->left)
q.push(root->left);
if (root->right)
q.push(root->right);
while (!q.empty())
{
node *temp = q.front();
q.pop();
cout << " " << temp->val;
if (temp->left)
q.push(temp->left);
if (temp->right)
q.push(temp->right);
}
return;
}
int main()
{
int n;
cin >> n;
int *post = new int[n];
int *in = new int[n];
for (int i = 0; i < n; i++)
cin >> post[i];
for (int i = 0; i < n; i++)
cin >> in[i];
node *root = build_tree(post, in, n);
eachfloor_visit(root);
return 0;
}
不建立二叉树,用index表示每个节点的输出顺序,假如根节点下标为i,那么左子树的节点的下标为(2i+1),右子树为(2i+2);用map保存下标和节点的val,最后输出map即可(思路参考柳婼)
dfs的思路都是在左子树和右子树的范围查找即可
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
vector<int> post, in;
map<int, int> res;
// 用下标记录节点的层级顺序,左(2i+1), 右边(2i+2),保存,最后排序输出
void dfs(int root, int begin, int end, int index)
{
if (begin > end)
return;
int i = begin;
while (i < end && post[root] != in[i])
i++;
res[index] = post[root];
// 遍历左子树
// root-(end-i)-1
dfs(root - end - 1 + i, begin, i - 1, index * 2 + 1);
dfs(root - 1, i + 1, end, index * 2 + 2);
}
int main()
{
int n;
cin >> n;
post.resize(n);
in.resize(n);
for (int i = 0; i < n; i++)
cin >> post[i];
for (int i = 0; i < n; i++)
cin >> in[i];
dfs(n - 1, 0, n - 1, 1);
map<int, int>::iterator it;
// auto it = res.begin();
it = res.begin();
cout << it->second;
// it++;
// while(it !=res.end()){
// cout << " " <<it->second;
// it++;
// }
while (++it != res.end())
cout << " " << it->second;
return 0;
}
总结
- vector重新设置个数大小,vector.resize(n)
- map的遍历:map<int, int>::iterator it;, it->second;