先建树可以吗,但是我的程序会崩溃,求帮忙找下错。。尴尬
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
void createTree(TreeNode* root, int data){
if (root == NULL){
root = new TreeNode(data);
}
else if (data < root->val)
createTree(root->left, data);
else if (data > root->val)
createTree(root->right, data);
else return;
}
void pre(TreeNode* root){
if (root){
cout << root->val << " ";
pre(root->left);
pre(root->right);
}
}
int main(){
int a[100];
int n;
TreeNode* root;
while (cin >> n){
root = NULL;
for (int i = 0; i < n; i++){
cin >> a[i];
createTree(root, a[i]);
}
pre(root);
}
    return 0;
}