WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions maximum path sum in a binary tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

#include<bits/stdc++.h>
using namespace std;


struct Node
{
int data;
struct Node* left, *right;
};


struct Node* newNode(int data)
{
struct Node* newNode = new Node;
newNode->data = data;
newNode->left = newNode->right = NULL;
return (newNode);
}


int findMaxUtil(Node* root, int &res)
{

if (root == NULL)
return 0;


int l = findMaxUtil(root->left,res);
int r = findMaxUtil(root->right,res);


int max_single = max(max(l, r) + root->data, root->data);


int max_top = max(max_single, l + r + root->data);

res = max(res, max_top);

return max_single;
}


int findMaxSum(Node *root)
{

int res = INT_MIN;


findMaxUtil(root, res);
return res;
}


int main(void)
{
struct Node *root = newNode(10);
root->left = newNode(2);
root->right = newNode(10);
root->left->left = newNode(20);
root->left->right = newNode(1);
root->right->right = newNode(-25);
root->right->right->left = newNode(3);
root->right->right->right = newNode(4);
cout << "Max path sum is " << findMaxSum(root);
return 0;
}