From ffa17b0df6b4d6438f61561a3ea493712cbf4c31 Mon Sep 17 00:00:00 2001 From: Nishtha987 <500094906@stu.upes.ac.in> Date: Wed, 6 Jul 2022 23:41:59 +0530 Subject: [PATCH] Day 21 --- nishtham/BST/README.md | 1 + nishtham/BST/mycode.c | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 nishtham/BST/README.md create mode 100644 nishtham/BST/mycode.c 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; +}