forked from fnplus/interview-techdev-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4300a4c
commit 87f26a3
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
struct node | ||
{ | ||
int data; | ||
struct node* left, *right; | ||
}; | ||
|
||
struct node* newNode(int data); | ||
int max(int a, int b); | ||
int height(struct node* node); | ||
|
||
int diameter(struct node * tree) | ||
{ | ||
|
||
if (tree == NULL) | ||
return 0; | ||
int lheight = height(tree->left); | ||
int rheight = height(tree->right); | ||
int ldiameter = diameter(tree->left); | ||
int rdiameter = diameter(tree->right); | ||
return max(lheight + rheight + 1, max(ldiameter, rdiameter)); | ||
} | ||
|
||
int height(struct node* node) | ||
{ | ||
|
||
if(node == NULL) | ||
return 0; | ||
|
||
return 1 + max(height(node->left), height(node->right)); | ||
} | ||
|
||
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); | ||
} | ||
|
||
int max(int a, int b) | ||
{ | ||
return (a >= b)? a: b; | ||
} | ||
|
||
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("Diameter of the given binary tree is %d\n", diameter(root)); | ||
|
||
getchar(); | ||
return 0; | ||
} |