Max Level Sum in Binary Tree
PROBLEM :
Given a Binary Tree having positive and negative nodes, the task is to find maximum sum level in it.
Examples:
Input : 4
/ \
2 -5
/ \ / \
-1 3 -2 6
Output: 6
Explanation :
Sum of all nodes of 0'th level is 4
Sum of all nodes of 1'th level is -3
Sum of all nodes of 0'th level is 6
Hence maximum sum is 6
Input : 1
/ \
2 3
/ \ \
4 5 8
/ \
6 7
Output : 17
Input:
The task is to complete the method which takes one argument, root of Binary Tree. The Node has a data part which stores the data, pointer to left child and pointer to right child. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function should return max sum level in the tree.
Constraints:
1 <=T<= 30
1 <=Number of nodes<= 100
1 <=Data of a node<= 1000
Example(To be used only for expected output):
Input:
2
2
1 2 L 1 3 R
6
4 2 L 4 -5 R 2 -1 L 2 3 R -5 -2 L -5 6 R
Output:
5
6
There are two test cases. First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 2 and right child of 1 is 3. Second test case represents a tree with 6 edges and 7 nodes.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Tree node structure used in the program
struct Node
{
int data;
Node* left, *right;
}; */
/*You are required to complete below method */
int heightTree(Node *) ;
void levelSum(Node *,int ,int *) ;
int maxLevelSum(Node * root)
{
if(root==NULL)
return 0;
int h,i,max,sum ;
h=heightTree(root) ;
max=INT_MIN ;
for(i=0;i<h;i++)
{
sum=0 ;
levelSum(root,i,&sum) ;
if(sum>max)
max=sum ;
}
return max ;
}
int heightTree(Node *root)
{
if(root==NULL)
return 0 ;
int left=heightTree(root->left) ;
int right=heightTree(root->right) ;
return left>right?left+1:right+1 ;
}
void levelSum(Node *root,int level,int *sum)
{
if(root==NULL)
return ;
if(level==0)
*sum=*sum+root->data ;
else
{
levelSum(root->left,level-1,&(*sum)) ;
levelSum(root->right,level-1,&(*sum)) ;
}
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(BETTER SOLUTION using STL)
--------------------------------------------------------------------------------
/* Tree node structure used in the program
struct Node
{
int data;
Node* left, *right;
}; */
/*You are required to complete below method */
int maxLevelSum(Node * root)
{
if(root==NULL)
return 0 ;
queue<Node*> que ;
que.push(root) ;
int sum,max ;
max=INT_MIN ;
while(!que.empty())
{
int count=que.size() ;
sum=0 ;
while(count--)
{
Node *temp=que.front() ;
sum+=temp->data ;
que.pop() ;
if(temp->left)
que.push(temp->left) ;
if(temp->right)
que.push(temp->right) ;
}
if(max<sum)
max=sum ;
}
return max ;
}
---------------------------------------------------------------------------------
Given a Binary Tree having positive and negative nodes, the task is to find maximum sum level in it.
Examples:
Input : 4
/ \
2 -5
/ \ / \
-1 3 -2 6
Output: 6
Explanation :
Sum of all nodes of 0'th level is 4
Sum of all nodes of 1'th level is -3
Sum of all nodes of 0'th level is 6
Hence maximum sum is 6
Input : 1
/ \
2 3
/ \ \
4 5 8
/ \
6 7
Output : 17
Input:
The task is to complete the method which takes one argument, root of Binary Tree. The Node has a data part which stores the data, pointer to left child and pointer to right child. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function should return max sum level in the tree.
Constraints:
1 <=T<= 30
1 <=Number of nodes<= 100
1 <=Data of a node<= 1000
Example(To be used only for expected output):
Input:
2
2
1 2 L 1 3 R
6
4 2 L 4 -5 R 2 -1 L 2 3 R -5 -2 L -5 6 R
Output:
5
6
There are two test cases. First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 2 and right child of 1 is 3. Second test case represents a tree with 6 edges and 7 nodes.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Tree node structure used in the program
struct Node
{
int data;
Node* left, *right;
}; */
/*You are required to complete below method */
int heightTree(Node *) ;
void levelSum(Node *,int ,int *) ;
int maxLevelSum(Node * root)
{
if(root==NULL)
return 0;
int h,i,max,sum ;
h=heightTree(root) ;
max=INT_MIN ;
for(i=0;i<h;i++)
{
sum=0 ;
levelSum(root,i,&sum) ;
if(sum>max)
max=sum ;
}
return max ;
}
int heightTree(Node *root)
{
if(root==NULL)
return 0 ;
int left=heightTree(root->left) ;
int right=heightTree(root->right) ;
return left>right?left+1:right+1 ;
}
void levelSum(Node *root,int level,int *sum)
{
if(root==NULL)
return ;
if(level==0)
*sum=*sum+root->data ;
else
{
levelSum(root->left,level-1,&(*sum)) ;
levelSum(root->right,level-1,&(*sum)) ;
}
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(BETTER SOLUTION using STL)
--------------------------------------------------------------------------------
/* Tree node structure used in the program
struct Node
{
int data;
Node* left, *right;
}; */
/*You are required to complete below method */
int maxLevelSum(Node * root)
{
if(root==NULL)
return 0 ;
queue<Node*> que ;
que.push(root) ;
int sum,max ;
max=INT_MIN ;
while(!que.empty())
{
int count=que.size() ;
sum=0 ;
while(count--)
{
Node *temp=que.front() ;
sum+=temp->data ;
que.pop() ;
if(temp->left)
que.push(temp->left) ;
if(temp->right)
que.push(temp->right) ;
}
if(max<sum)
max=sum ;
}
return max ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment