Find the maximum sum leaf to root path in a Binary Tree
PROBLEM :
Given a Binary Tree, find the maximum sum from a leaf to root. For example, in the following tree, there are three leaf to root paths 8->-2->10, -4->-2->10 and 7->10. The sums of these three paths are 16, 4 and 17 respectively. The maximum of them is 17.
10
/ \
-2 7
/ \
8 -4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
typedef struct BST
{
int info ;
struct BST *left ;
struct BST *right ;
}tree ;----------------------------------------*/
int height_tree(tree *) ;
void path_array(tree *,int [],int ,int *) ;
int array_sum(int [],int ) ;
int max_sum_path(tree *root)
{
if(root==NULL)
return ;
int h=height_tree(root) ;
int a[h] ;
int k=0,max=0 ;
path_array(root,a,k,&max) ;
return max ;
}
int height_tree(tree *root)
{
if(root==NULL)
return 0 ;
int L_len,R_len ;
L_len=height_tree(root->left) ;
R_len=height_tree(root->right) ;
if(L_len>R_len)
return(1+L_len) ;
else
return(1+R_len) ;
}
void path_array(tree *root,int a[],int k,int *max)
{
if(root==NULL)
return ;
if(root->left==NULL&&root->right==NULL)
{
a[k++]=root->info ;
int sum ;
sum=array_sum(a,k) ;
if(sum>(*max))
(*max)=sum ;
return ;
}
else
a[k++]=root->info ;
path_array(root->left,a,k,&(*max)) ;
path_array(root->right,a,k,&(*max)) ;
}
int array_sum(int a[],int k)
{
int s=0 ,i ;
for(i=0;i<k;i++)
s=s+a[i] ;
return s ;
}
---------------------------------------------------------------------------------
Given a Binary Tree, find the maximum sum from a leaf to root. For example, in the following tree, there are three leaf to root paths 8->-2->10, -4->-2->10 and 7->10. The sums of these three paths are 16, 4 and 17 respectively. The maximum of them is 17.
10
/ \
-2 7
/ \
8 -4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
typedef struct BST
{
int info ;
struct BST *left ;
struct BST *right ;
}tree ;----------------------------------------*/
int height_tree(tree *) ;
void path_array(tree *,int [],int ,int *) ;
int array_sum(int [],int ) ;
int max_sum_path(tree *root)
{
if(root==NULL)
return ;
int h=height_tree(root) ;
int a[h] ;
int k=0,max=0 ;
path_array(root,a,k,&max) ;
return max ;
}
int height_tree(tree *root)
{
if(root==NULL)
return 0 ;
int L_len,R_len ;
L_len=height_tree(root->left) ;
R_len=height_tree(root->right) ;
if(L_len>R_len)
return(1+L_len) ;
else
return(1+R_len) ;
}
void path_array(tree *root,int a[],int k,int *max)
{
if(root==NULL)
return ;
if(root->left==NULL&&root->right==NULL)
{
a[k++]=root->info ;
int sum ;
sum=array_sum(a,k) ;
if(sum>(*max))
(*max)=sum ;
return ;
}
else
a[k++]=root->info ;
path_array(root->left,a,k,&(*max)) ;
path_array(root->right,a,k,&(*max)) ;
}
int array_sum(int a[],int k)
{
int s=0 ,i ;
for(i=0;i<k;i++)
s=s+a[i] ;
return s ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment