Skip to content

Commit

Permalink
Merge pull request #2320 from spaceman-dev/patch-2
Browse files Browse the repository at this point in the history
Create MaximumPathSumBetweenTwoLeafNodes.cpp
  • Loading branch information
fineanmol authored Oct 4, 2022
2 parents 58f201d + 52a139d commit 771025b
Showing 1 changed file with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

// Tree Node
struct Node {
int data;
Node *left;
Node *right;

Node(int val) {
data = val;
left = right = NULL;
}
};

// Function to Build Tree
Node *buildTree(string str) {
// Corner Case
if (str.length() == 0 || str[0] == 'N') return NULL;

// Creating vector of strings from input
// string after spliting by space
vector<string> ip;

istringstream iss(str);
for (string str; iss >> str;) ip.push_back(str);

// Create the root of the tree
Node *root = new Node(stoi(ip[0]));

// Push the root to the queue
queue<Node *> queue;
queue.push(root);

// Starting from the second element
int i = 1;
while (!queue.empty() && i < ip.size()) {

// Get and remove the front of the queue
Node *currNode = queue.front();
queue.pop();

// Get the current Node's value from the string
string currVal = ip[i];

// If the left child is not null
if (currVal != "N") {

// Create the left child for the current Node
currNode->left = new Node(stoi(currVal));

// Push it to the queue
queue.push(currNode->left);
}

// For the right child
i++;
if (i >= ip.size()) break;
currVal = ip[i];

// If the right child is not null
if (currVal != "N") {

// Create the right child for the current Node
currNode->right = new Node(stoi(currVal));

// Push it to the queue
queue.push(currNode->right);
}
i++;
}

return root;
}


// } Driver Code Ends
/*
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/

class Solution
{
public:
int findMaxPathSum(Node* root, int &res)
{
if(!root)
return 0;
if(!root->left && !root->right)
return root->data;
int left = findMaxPathSum(root->left, res);
int right = findMaxPathSum(root->right, res);
if(root->left && root->right)
{
res = max(res, left + right + root->data);
return max(left, right) + root->data;
}
return root->left ? left + root->data : right + root->data;
}
int maxPathSum(Node* root)
{
// code here
int res = INT_MIN;
int val = findMaxPathSum(root, res);
return res == INT_MIN ? val : res;
}
};

// { Driver Code Starts.

int main() {
int tc;
scanf("%d ", &tc);
while (tc--) {
string treeString;
getline(cin, treeString);
Node *root = buildTree(treeString);
Solution ob;
cout << ob.maxPathSum(root) << "\n";
}
return 0;
} // } Driver Code Ends

0 comments on commit 771025b

Please sign in to comment.