jayati

Maximum Path Sum

Oct 12th, 2023
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // Tree Node
  6. struct Node {
  7.     int data;
  8.     Node *left;
  9.     Node *right;
  10.  
  11.     Node(int val) {
  12.         data = val;
  13.         left = right = NULL;
  14.     }
  15. };
  16.  
  17. // Function to Build Tree
  18. Node *buildTree(string str) {
  19.     // Corner Case
  20.     if (str.length() == 0 || str[0] == 'N') return NULL;
  21.  
  22.     // Creating vector of strings from input
  23.     // string after spliting by space
  24.     vector<string> ip;
  25.  
  26.     istringstream iss(str);
  27.     for (string str; iss >> str;) ip.push_back(str);
  28.  
  29.     // Create the root of the tree
  30.     Node *root = new Node(stoi(ip[0]));
  31.  
  32.     // Push the root to the queue
  33.     queue<Node *> queue;
  34.     queue.push(root);
  35.  
  36.     // Starting from the second element
  37.     int i = 1;
  38.     while (!queue.empty() && i < ip.size()) {
  39.  
  40.         // Get and remove the front of the queue
  41.         Node *currNode = queue.front();
  42.         queue.pop();
  43.  
  44.         // Get the current Node's value from the string
  45.         string currVal = ip[i];
  46.  
  47.         // If the left child is not null
  48.         if (currVal != "N") {
  49.  
  50.             // Create the left child for the current Node
  51.             currNode->left = new Node(stoi(currVal));
  52.  
  53.             // Push it to the queue
  54.             queue.push(currNode->left);
  55.         }
  56.  
  57.         // For the right child
  58.         i++;
  59.         if (i >= ip.size()) break;
  60.         currVal = ip[i];
  61.  
  62.         // If the right child is not null
  63.         if (currVal != "N") {
  64.  
  65.             // Create the right child for the current Node
  66.             currNode->right = new Node(stoi(currVal));
  67.  
  68.             // Push it to the queue
  69.             queue.push(currNode->right);
  70.         }
  71.         i++;
  72.     }
  73.  
  74.     return root;
  75. }
  76.  
  77.  
  78. // } Driver Code Ends
  79. /*
  80. struct Node
  81. {
  82.     int data;
  83.     struct Node* left;
  84.     struct Node* right;
  85.    
  86.     Node(int x){
  87.         data = x;
  88.         left = right = NULL;
  89.     }
  90. };
  91. */
  92.  
  93. class Solution {
  94. public:
  95.     int findmax(Node* root,int &ans)
  96.     {
  97.         if(root->left==NULL && root->right==NULL)
  98.         {
  99.             return root->data;
  100.         }
  101.         if(root->right==NULL)
  102.         {
  103.             return (findmax(root->left,ans) + root->data);
  104.         }
  105.         if(root->left==NULL)
  106.         {
  107.             return (findmax(root->right,ans) + root->data);
  108.         }
  109.        
  110.        
  111.         int leftsum = findmax(root->left,ans);
  112.         int rightsum = findmax(root->right,ans);
  113.        
  114.         ans = max(ans,root->data+leftsum+rightsum);
  115.        
  116.         return (root->data+max(leftsum,rightsum));
  117.     }
  118.     int maxPathSum(Node* root)
  119.     {
  120.         // code here
  121.         int ans=INT_MIN;
  122.         int t=findmax(root,ans);
  123.         if(root->left==NULL || root->right==NULL)
  124.         {
  125.             ans = max(ans,t);
  126.         }
  127.         return ans;
  128.        
  129.        
  130.     }
  131. };
  132.  
  133. //{ Driver Code Starts.
  134.  
  135. int main() {
  136.     int tc;
  137.     scanf("%d ", &tc);
  138.     while (tc--) {
  139.         string treeString;
  140.         getline(cin, treeString);
  141.         Node *root = buildTree(treeString);
  142.         Solution ob;
  143.         cout << ob.maxPathSum(root) << "\n";
  144.     }
  145.     return 0;
  146. }
  147. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment