Created
August 27, 2014 23:57
-
-
Save vinay13/c102c68247786ef2e033 to your computer and use it in GitHub Desktop.
given a binary tree where each node has some weight. You have to return the max weight in the binary tree.
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
//binary tree where each node has some weight. | |
//you have to return the max weight int the binary tree | |
#include <stdio.h> | |
#include <malloc.h> | |
struct node | |
{ | |
int data; | |
struct node *left,*right; | |
}; | |
struct node *tree(int k) | |
{ | |
struct node *root=(struct node*)malloc(sizeof(struct node)); | |
root->data=k; | |
root->left=root->right=NULL; | |
return root; | |
} | |
int MaxWeight(struct node *root) | |
{ | |
if(root!=NULL) | |
return root->data+MaxWeight(root->left)+MaxWeight(root->right); | |
else | |
return 0; | |
} | |
int main() | |
{ | |
struct node *root=NULL; | |
int weight; | |
root=tree(2); | |
root->left=tree(-1); | |
root->right=tree(3); | |
//int root_node=root->data; | |
weight=MaxWeight(root); | |
printf("%d ",weight ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment