GeeksforGeeks » Trees specific questions

Replace root value with the sum of left and right node value for every nodes

(3 posts)
  • Started 1 year ago by Shashi
  • Latest reply from shashi_kumar

Tags:

  1. Shashi
    guest
    Posted 1 year ago #

    A binary tree is given. Write a C code such that the code should put the sum of left and right nodes at root for every nodes of the tree.

  2. kartik
    Moderator
    Posted 1 year ago #

  3. shashi_kumar
    Member
    Posted 9 months ago #

    That's pretty easy !!....But what if you cant decrease the value of any node..You can only increase the value...withot altering the Binary tree structure...
    here is the code that work fine....

    #include<stdio.h>
    #include<stdlib.h>
    struct node
    {
      int data;
      int sum;
      struct node *left;
      struct node *right;
    }*root;
    
    void balance(struct node *r,int diff)
    {
      if(r->left!=NULL)
      { if (r->left->left!=NULL ||r->left->right!=NULL)
        {    r->left->data+=diff/2;
    	 balance(r->left,diff/2);
        }
        else
        {	r->left->data+=diff;
        }
      }
      else if(r->right!=NULL)
      {
        if (r->right->left!=NULL ||r->right->right!=NULL)
        {    r->right->data+=diff/2;
    	 balance(r->right,diff/2);
        }
        else
        {	r->right->data+=diff;
        }
      }
    }
    void makesumtree(struct node *np)
    {
      if(np!=NULL)
      {
    
        if(np->left!=NULL)
         makesumtree(np->left);
        if(np->right!=NULL)
          makesumtree(np->right);
    
        np->sum=0;
    
        if(np->left!=NULL)
        {	if (np->left->left!=NULL ||np->left->right!=NULL)
    	  np->sum+=2*np->left->data;
    	else
    	 np->sum+=np->left->data;
        }
         if(np->right!=NULL)
        {	if (np->right->left!=NULL ||np->right->right!=NULL)
    	  np->sum+=2*np->right->data;
          else
    	 np->sum+=np->right->data;
        }
    
        if(np->sum >=np->data)
          np->data=np->sum;
        else
          balance(np,np->data-np->sum);
    
      }
    }
    
    void traversal(struct node *n)
    {
      if(n!=NULL)
      {
    
          traversal(n->left);
        printf("%d ",n->data);
        traversal(n->right);
      }
    }
    struct node *createnode(int k)
    {
      struct node *nwnode=malloc(sizeof(struct node));
      nwnode->data=k;
      nwnode->left=NULL;
      nwnode->right=NULL;
      return nwnode;
    }
    int main(int args,char *argv[])
    {
     root=createnode(54);
      root->left=createnode(4);
      root->right=createnode(18);
      root->left->left=createnode(2);
      root->left->right=createnode(3);
      root->left->left->left=createnode(2);
      root->right->left=createnode(9);
      root->right->right=createnode(7);
     printf("the inorder traversal of the given graph is ");
     traversal(root);
     printf("\n");
    
     makesumtree(root);
     printf("\n");
     traversal(root);
     printf("\n");
     printf("\n");
    }

Reply

You must log in to post.

RSS feed for this topic