You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
1.4 KiB

/*
* tree03.cpp
*
* Created on: Apr 12, 2024
* Author: 28032
*/
//建树
/*#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
struct Note
{
Note(int val)
{
value = val;
left = NULL;
right = NULL;
}
char value;
Note* left;
Note* right;
};
string front,middle;
set<char> exist;
Note* reBuildTree(unsigned int f_index,int m_l,int m_r)
{
char rootValue = front[f_index];
while(exist.count(rootValue)&&f_index<front.size())
rootValue = front[++f_index];
Note* root = new Note(rootValue);
exist.insert(rootValue);
int m_index = middle.find(rootValue);
string L = middle.substr(m_l,m_index-m_l);
if(L.size() > 1)
root->left = reBuildTree(f_index+1,m_l,m_index-1);
else if(L.size() == 1)
{
exist.insert(L[0]);
root->left = new Note(L[0]);
}
else
root->left = NULL;
string R = middle.substr(m_index+1,m_r-m_index);
if(R.size() > 1)
root->right = reBuildTree(f_index+1,m_index+1,m_r);
else if(R.size() == 1)
{
exist.insert(R[0]);
root->right = new Note(R[0]);
}
else
root->right = NULL;
return root;
}
void backVisit(Note* root)
{
if(root == NULL) return ;
backVisit(root->left);
backVisit(root->right);
cout<<root->value;
}
int main()
{
while(1)
{
int n = 0;
cin>>n;
if(!n) break;
cin>>front>>middle;
exist.clear();
Note* root = reBuildTree(0,0,n-1);
backVisit(root);
cout<<endl;
}
return 0;
}*/