Getting wrong ans

in preoder i am getting a garbage value. i dont know how. can you please tell me what is wrong in my code and how to correct my code

Your build bst function is not correct.
Follow this

node* insertnode(int d, node*startnode) {
		if (startnode == NULL) {
			node*nn = new node(d);
			return nn;
		}

		if (startnode->data > d) {
			startnode->left = insertnode(d, startnode->left);
		}

		else {
			startnode->right = insertnode(d, startnode->right);
		}
		return startnode;
	}
	node* BuildTree(int *arr, int n) {
		for (int i = 0; i < n; i++) {
			rootnode = insertnode(arr[i], rootnode);
		}
        return rootnode;
	}

https://ide.codingblocks.com/s/287141 getting segmentation fault. And can you please tell me hoe my logic is wrong because i am getting correct ans with an garbage value