#include<iostream>
using namespace std;
class node {
public:
int data = 0;
node* left = NULL;
node* right = NULL;
node(int d) {
data = d;
}
};
void printPre(node *root) {
if (root == NULL) {
return;
}
// print root
cout << root->data << " ";
// Call on left part
printPre(root->left);
// Call on right part
printPre(root->right);
}
node* buildTree(int a[], int s, int e) {
// Base Case
if (s >= e) {
return NULL;
}
int mid = (s + e) / 2;
node* root = new node(a[mid]);
root->left = buildTree(a, s, mid);
root->right = buildTree(a, mid + 1, e);
return root;
}
void solve() {
int n; cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
node* root = buildTree(a, 0, n);
printPre(root);
cout << endl;
}
int main() {
int t; cin >> t;
while (t--) {
solve();
}
}
Build bst -aint passing tc?
@darkthunderassassin_818a6616c3c74f13,
code was almost correct, small corrections done here https://ide.codingblocks.com/s/657892
hope this helps
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.