给你一个满二叉树的层次遍历序列,请编程输出该二叉树的前序遍历序列.
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct TreeNode {
string val;
TreeNode* left;
TreeNode* right;
TreeNode(string val) {
this->val = val;
this->left = this->right = NULL;
}
};
void preOrder(TreeNode *root)
{
if (!root)
{
return;
}
cout << root->val;
preOrder(root->left);
preOrder(root->right);
}
int main()
{
int n;
cin >> n;
string node_val;
vector<string> node_vec;
for (int i = 0; i < n; i++)
{
cin >> node_val;
node_vec.push_back(node_val);
}
int i = 0;
TreeNode* root = new TreeNode(node_vec[i]);
i++;
vector <TreeNode*> list;
list.push_back(root);
TreeNode *cur;
TreeNode* node;
while (list.size()!=0 && i<=n-1)
{
cur = list[0];
list.erase(list.begin());
if (node_vec[i]=="#")
{
cur->left = NULL;
}
else
{
node = new TreeNode(node_vec[i]);
cur->left = node;
list.push_back(cur->left);
}
if (node_vec[i+1] == "#")
{
cur->right = NULL;
}
else
{
node = new TreeNode(node_vec[i + 1]);
cur->right = node;
list.push_back(cur->right);
}
i=i+2;
}preOrder(root);
}