diff --git a/nishtham/BST/README.md b/nishtham/BST/README.md new file mode 100644 index 0000000..9e97696 --- /dev/null +++ b/nishtham/BST/README.md @@ -0,0 +1 @@ +ques: https://www.hackerrank.com/challenges/binary-search-tree-insertion/problem?isFullScreen=true \ No newline at end of file diff --git a/nishtham/BST/mycode.c b/nishtham/BST/mycode.c new file mode 100644 index 0000000..8927ddd --- /dev/null +++ b/nishtham/BST/mycode.c @@ -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; +}