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 shradha mudgil/Height of a Binary Tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Question: https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem?isFullScreen=true
84 changes: 84 additions & 0 deletions shradha mudgil/Height of a Binary Tree/code.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

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;
}