Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nishtham/BST/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ques: https://www.hackerrank.com/challenges/binary-search-tree-insertion/problem?isFullScreen=true
65 changes: 65 additions & 0 deletions nishtham/BST/mycode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
struct node {

int data;
struct node *left;
struct node *right;

};

void preOrder( struct node *root) {

if( root == NULL )
return;
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);

}

/* you only have to complete the function given below.
node is defined as

struct node {

int data;
struct node *left;
struct node *right;

};

*/
struct node* insert( struct node* root, int data ) {
if(root == NULL){
root = malloc(sizeof(struct node));
root->data = data;
root->left = NULL;
root->right = NULL;

}else if(data < root->data){
root->left = insert(root->left, data);
}else{
root->right = insert(root->right, data);
}
return root;


}


int main() {

struct node* root = NULL;

int t;
int data;

scanf("%d", &t);

while(t-- > 0) {
scanf("%d", &data);
root = insert(root, data);
}

preOrder(root);
return 0;
}