void search_path( root , int i )
{
       static char str[100];
       if(root == null)
         return;

       if( i == 0 )
          str[i] = root->data;

       else  str[i] = str[i-1] + root->data; // Maintaining the cumulative
sum of all the sums till now.

       if ( str[i] == value )
       {
              // A path has been found. As the cumulative sum till now is
equal to the value.
              print_path(str , i , 0);
       }

        else
        {
                // Check for an embedded path. As u said a path may not
start from root.
                for( int j=0 ; j<i ; ++j)
                {
                       if ( (str[i] - str[j]) == value )
                       {
                                  print_path(str , i , 0);
                                  break;
                       }
                }
        } //else

         search_path(root->left , i+1);
         search_path(root->right , i+1);
}

void print_path(char str[] , int i , int sum) // just printing the path in
top to down manner.. Using recursion.
{
          int temp = str[i] - str[i-1] ;
          sum = sum + temp;
          if( sum == value )
          {
                    cout << temp;
                    return;
          }

          print_path( str , i-1 , sum);
          cout << temp;
}

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to