Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* A binary tree node has data, pointer to left child
- and a pointer to right child */
- struct node
- {
- int data;
- struct node* left, *right;
- };
- /* function to create a new node of tree and returns pointer */
- struct node* newNode(int data)
- {
- struct node* node = (struct node*)
- malloc(sizeof(struct node));
- node->data = data;
- node->left = NULL;
- node->right = NULL;
- return(node);
- }
- void fsrednica(struct node* d,int* sciezka,int* srednica) {
- int sciezka1,sciezka2,srednica1,srednica2,pom;
- if(d==NULL) {
- *sciezka=-1;
- *srednica=0;
- }
- else {
- fsrednica(d->left,&sciezka1,&srednica1);
- fsrednica(d->right,&sciezka2,&srednica2);
- *sciezka = (sciezka1>sciezka2) ? sciezka1+1 : sciezka2+1;
- pom = (srednica1>srednica2) ? srednica1 : srednica2;
- *srednica = (pom>(sciezka1+sciezka2+2)) ? pom : (sciezka1+sciezka2+2);
- }
- }
- int srednicaDrzewa(struct node* d) {
- int sciezka;
- int srednica;
- fsrednica(d,&sciezka,&srednica);
- return srednica;
- }
- int main()
- {
- struct node *root = newNode(1);
- root->left = newNode(2);
- root->right = newNode(3);
- root->left->left = newNode(4);
- root->left->right = newNode(5);
- printf("%d",srednicaDrzewa(root));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement