My issue
I use one data structure called binary tree but it shows total heigth
how to approach each node
My code
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* createNode(int data) {
struct node* newnode = malloc(sizeof(struct node));
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
struct node* insert(struct node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (root->data > data) {
root->right = insert(root->right, data);
} else {
root->left = insert(root->left, data);
}
return root;
}
int height(struct node* root) {
if (root == NULL) {
return 0;
}
int right = height(root->right);
int left = height(root->left);
if (right > left) {
return right + 1;
} else {
return left + 1;
}
}
int main() {
struct node* root = NULL;
int n, data;
scanf("%d", &n);
while (n--) {
scanf("%d", &data);
root = insert(root, data);
}
int k = height(root);
printf("%d", k);
return 0;
}
Problem Link: Coins And Triangle Practice Coding Problem - CodeChef