-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2320 from spaceman-dev/patch-2
Create MaximumPathSumBetweenTwoLeafNodes.cpp
- Loading branch information
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
Program's_Contributed_By_Contributors/C++/MaximumPathSumBetweenTwoLeafNodes.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |