Advertisement
Guest User

maxSumPath

a guest
Mar 29th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include<iostream>
  4. #include<cstdio>
  5. #include<vector>
  6.  
  7. struct Node {
  8.    
  9.     Node() {
  10.         value = 0;
  11.         left = NULL;
  12.         right = NULL;
  13.         timesVisited = 0;
  14.     }
  15.    
  16.     int value;
  17.     Node* left;
  18.     Node* right;
  19.     int timesVisited;
  20.    
  21. };
  22.  
  23. void constructTree(Node* head) {
  24.    
  25.     if (head == NULL) {
  26.         return ;
  27.     }
  28.    
  29.     head->value = 10;
  30.     head->left=new Node;
  31.     head->right=new Node;
  32.    
  33.    
  34.     head->left->value = 5;
  35.     head->left->left=new Node;
  36.     head->left->right=new Node;
  37.     head->left->left->value = 1;
  38.     head->left->right->value=8;
  39.    
  40.     head->right->value = 40;
  41.     head->right->left=new Node();
  42.     head->right->right=new Node();
  43.     head->right->left->value=30;
  44.     head->right->left->left = new Node();
  45.     head->right->left->left->value = 20;
  46.     head->right->right->value=50;
  47. }
  48.  
  49. int maxSum(Node* head,int sum) {
  50.  
  51.   if(head == NULL) {
  52.     return sum;
  53.   }
  54.  
  55.   int left = maxSum(head->left, sum + head->value) ;
  56.   int right = maxSum(head->right, sum + head->value);
  57.  
  58.   return left>right?left:right;
  59.    
  60. }
  61.  
  62. bool foundPath = false;
  63. vector<Node*> sumVec;
  64.  
  65. void maxPath(Node* parent, Node* head, int maxSum, int k) {
  66.  
  67.  if(head == NULL) {
  68.    
  69.     if(k == maxSum) {
  70.       bool foundBefore = false;
  71.      
  72.       if(!sumVec.empty()) {
  73.        
  74.         for(int i = 0; i < sumVec.size(); i++ ) {
  75.           Node* node = sumVec[i];
  76.          
  77.           if(node == parent) {
  78.             foundBefore = true;
  79.             break;
  80.           }
  81.         }
  82.       }
  83.      
  84.       if(!foundBefore) {
  85.         foundPath = true;
  86.         sumVec.push_back(parent);
  87.       }
  88.     }
  89.   return;
  90.  }
  91.  
  92.   maxPath(head, head->left, maxSum, k +  head->value);
  93.  
  94.   if(foundPath) {
  95.     cout << head->value << " ";
  96.     return ;
  97.   }
  98.  
  99.   maxPath(head, head->right, maxSum, k + head->value);
  100.  
  101.   if(foundPath) {
  102.     cout << head->value << " ";
  103.     return ;
  104.   }
  105. }
  106.  
  107.  
  108. int main() {
  109.  
  110.   Node* head ;
  111.   head = new Node;
  112.   constructTree(head);
  113.  
  114.   int maxSumTree = maxSum(head, 0);
  115.  
  116.   cout << maxSumTree << endl;
  117.   do {
  118.     foundPath = false;
  119.     maxPath(NULL, head, maxSumTree, 0);
  120.     cout << endl;
  121.   }while(foundPath);
  122.  
  123.   cin>>maxSumTree;
  124.  
  125.  
  126.   return 0;
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement