解法:
#include<iostream>
using namespace std;
struct treeNode {
char val;
treeNode* left, * right;
treeNode(char x) :val(x), left(NULL), right(NULL) {};
};
treeNode* buildtree() {
char ch;
cin >> ch;
if (ch == '#') return NULL;
treeNode* r = new treeNode(ch);
r->left = buildtree();
r->right = buildtree();
return r;
}
void hdfs(treeNode* root) {
if (root == NULL) return;
hdfs(root->left);
hdfs(root->right);
swap(root->left, root->right);
return;
}
void zdfs(treeNode* root) {
if (root == NULL) return;
zdfs(root->left);
cout << root->val;
zdfs(root->right);
return;
}
void qdfs(treeNode* root) {
if (root == NULL) return;
cout << root->val;
qdfs(root->left);
qdfs(root->right);
return;
}
int main() {
treeNode* root = buildtree();
hdfs(root);
zdfs(root);
cout << endl;
qdfs(root);
return 0;
}