12.23
include
include
using namespace std;
struct TreeNode{
char val;
TreeNode* left;
TreeNode* right;
TreeNode(char x):val(x),left(nullptr),right(nullptr){}
};
int idx=0;
TreeNode* buildTree(const string &s){
if(idx >= s.size()||s[idx]'#'){
idx++;
return nullptr;
}
TreeNoderoot=new TreeNode(s[idx++]);
root->left=buildTree(s);
root->right=buildTree(s);
return root;
}
void inorder(TreeNode root,string& res){
if(rootnullptr)return ;
inorder(root->left,res);
res+=root->val;
inorder(root->right,res);
}
void swapChildren(TreeNode* root){
if(root==nullptr)return ;
swap(root->left,root->right);
swapChildren(root->left);
swapChildren(root->right);
}
int main(){
string s;
getline(cin,s);
idx=0;
TreeNode* root=buildTree(s);
string originInorder;
inorder(root,originInorder);
cout<<originInorder<<endl;swapChildren(root);
string swappedInorder;
inorder(root,swappedInorder);
cout<<swappedInorder;
return 0;
}

浙公网安备 33010602011771号