#include <iostream>
using namespace std;
struct Tree {
int data;
struct Tree* lchild, * rchild;
};
int *a;
struct Tree* T;
Tree* creatTree(int a[], int i) {
//创建树
if (a[i]) {
Tree* p = new Tree;
p->data = a[i];
p->lchild = creatTree(a, 2 * i);
p->rchild = creatTree(a, 2 * i + 1);
return p;
}
else {
return NULL;
}
}
//先序遍历
void Pro(Tree* t) {
if (t) {
cout << (int)t->data << " ";
Pro(t->lchild);
Pro(t->rchild);
}
}
void ChangeTree(Tree *a){
if (a) {
if (a->lchild || a->rchild) {
ChangeTree(a->lchild);
ChangeTree(a->rchild);
Tree *temp;
temp = a->lchild;
a->lchild = a->rchild;
a->rchild = temp;
}
}
}
int main() {
Tree* root;
int a[50] = { 0,1,2,3,4,5,6,7,8,9 };
root = creatTree(a, 1);
cout << "先序遍历序号:" << endl;
Pro(root);
cout << endl;
ChangeTree(a);
cout << "序号:" << endl;
Pro(root);
cout << endl;
system("pause");
return 0;
}
这里的ChangeTree(a)显示int*实参与Tree*形参不兼容,请问该如何解决呢?
这里的本意应该是传入root而不是a,应改为ChangeTree(root)