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(root
nullptr)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;
}

posted @ 2025-12-25 08:53  Cx330。  阅读(2)  评论(0)    收藏  举报