From bd86ae1c1ba60f7c82fec493fbb8570e7cbccf4d Mon Sep 17 00:00:00 2001 From: shradha789 <500094907@stu.upes.ac.in> Date: Wed, 6 Jul 2022 23:57:18 +0530 Subject: [PATCH] Day21 DS --- .../Height of a Binary Tree/README.md | 1 + shradha mudgil/Height of a Binary Tree/code.c | 84 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 shradha mudgil/Height of a Binary Tree/README.md create mode 100644 shradha mudgil/Height of a Binary Tree/code.c diff --git a/shradha mudgil/Height of a Binary Tree/README.md b/shradha mudgil/Height of a Binary Tree/README.md new file mode 100644 index 0000000..1e5c418 --- /dev/null +++ b/shradha mudgil/Height of a Binary Tree/README.md @@ -0,0 +1 @@ +Question: https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem?isFullScreen=true \ No newline at end of file diff --git a/shradha mudgil/Height of a Binary Tree/code.c b/shradha mudgil/Height of a Binary Tree/code.c new file mode 100644 index 0000000..b834fb2 --- /dev/null +++ b/shradha mudgil/Height of a Binary Tree/code.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +struct node { + + int data; + struct node *left; + struct node *right; + +}; + +struct node* insert( struct node* root, int data ) { + + if(root == NULL) { + + struct node* node = (struct node*)malloc(sizeof(struct node)); + + node->data = data; + + node->left = NULL; + node->right = NULL; + return node; + + } else { + + struct node* cur; + + if(data <= root->data) { + cur = insert(root->left, data); + root->left = cur; + } else { + cur = insert(root->right, data); + root->right = cur; + } + + return root; + } +} + + + +struct node { + + int data; + struct node *left; + struct node *right; + +}; + +int getHeight(struct node* root) { + int left_h,right_h; + if(root==NULL) + return -1; + else + { + left_h = getHeight(root->left) + 1; + right_h = getHeight(root->right) + 1; + } + if(left_h > right_h) + return (left_h); + else + return (right_h ); +} + + +int main() { + + struct node* root = NULL; + + int t; + int data; + + scanf("%d", &t); + + while(t-- > 0) { + scanf("%d", &data); + root = insert(root, data); + } + + printf("%d",getHeight(root)); + return 0; +}