题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/6e732a9632bc4d12b442469aed7fe9ce
#include <iostream>
using namespace std;
struct BTree
{
char val;
struct BTree*lchild,*rchild;
BTree(char c):val(c),lchild(nullptr),rchild(nullptr){};
};
BTree* createBTree(string str1,string str2)
{
if(str1.length()==0)
{
return nullptr;;
}
char c=str1[0];
int pos=str2.find(c);
BTree*ret=new BTree(c);
ret->lchild=createBTree(str1.substr(1,pos), str2.substr(0,pos));
ret->rchild=createBTree(str1.substr(pos+1), str2.substr(pos+1));
return ret;
}
void postorder(BTree*root)
{
if(root)
{
postorder(root->lchild);
postorder(root->rchild);
cout<<root->val;
}
}
int main() {
string str1;
string str2;
while(cin>>str1>>str2)
{
BTree* T=createBTree(str1,str2);
postorder(T);
cout<<endl;
}
}
// 64 位输出请用 printf("%lld")

