Advertisement
i_love_rao_khushboo

Max Path Sum Between any 2 Nodes in a BT.cpp

Mar 31st, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. /*PROBLEM: Given a binary tree, find the maximum path sum. The path may start and end at any
  2.            node in the tree.
  3.            NOTE: The value of a node can be any integer.
  4.   Link: https://www.interviewbit.com/problems/max-sum-path-in-binary-tree/
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9.  
  10. #define ll long long
  11. #define ull unsigned long long
  12. #define pb push_back
  13. #define mp make_pair
  14. #define F first
  15. #define S second
  16. #define PI 3.1415926535897932384626
  17. #define deb(x) cout << #x << "=" << x << endl
  18. #define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
  19. typedef pair<int, int> pii;
  20. typedef pair<ll, ll> pll;
  21. typedef vector<int> vi;
  22. typedef vector<ll> vll;
  23. typedef vector<ull> vull;
  24. typedef vector<pii> vpii;
  25. typedef vector<pll> vpll;
  26. typedef vector<vi> vvi;
  27. typedef vector<vll> vvll;
  28. typedef vector<vull> vvull;
  29. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  30. int rng(int lim) {
  31.     uniform_int_distribution<int> uid(0,lim-1);
  32.     return uid(rang);
  33. }
  34.  
  35. const int INF = 0x3f3f3f3f;
  36. const int mod = 1e9+7;
  37.  
  38. class TreeNode {
  39.   public:
  40.     int val;
  41.     TreeNode *left;
  42.     TreeNode *right;
  43.     TreeNode(): val(0), left(NULL), right(NULL) {}
  44.     TreeNode(int data): val(data), left(NULL), right(NULL) {}
  45.     TreeNode(int data, TreeNode *left, TreeNode *right): val(data), left(left), right(right) {}
  46. };
  47.  
  48. ll maxPathSumBwAnyTwoNodes(TreeNode *root, ll &res) {
  49.   // base condition
  50.     if(root == NULL) return 0LL;
  51.    
  52.     // hypothesis
  53.     ll l = maxPathSumBwAnyTwoNodes(root->left, res);
  54.     ll r = maxPathSumBwAnyTwoNodes(root->right, res);
  55.    
  56.     // induction
  57.     // remember temp is used whenever the current node wants to pass it's
  58.     // constribution to it's parent node
  59.     ll temp = max(max(l, r) + root->val, (ll)root->val); // taken in consideration the case
  60.                                                          // when l & r both can be -ve
  61.    
  62.     // l + r + root->val indicates the current nodes want's the result to pass
  63.     // through it
  64.     ll ans = max(temp, l + r + root->val);
  65.    
  66.     // updating the final result
  67.     res = max(res, ans);
  68.  
  69.     return temp;
  70. }
  71.  
  72. void solve()
  73. {
  74.   TreeNode* root = new TreeNode(1);
  75.   root->left = new TreeNode(2);
  76.   root->right = new TreeNode(3);
  77.   root->right->left = new TreeNode(4);
  78.   root->right->right = new TreeNode(5);
  79.   root->right->left->left = new TreeNode(6);
  80.   root->right->left->right = new TreeNode(7);
  81.   root->right->left->right->left = new TreeNode(8);
  82.   root->right->left->right->right = new TreeNode(9);
  83.  
  84.   ll res = LLONG_MIN;
  85.   maxPathSumBwAnyTwoNodes(root, res);
  86.  
  87.   cout << res << "\n";
  88. }
  89.  
  90. int main()
  91. {
  92.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  93.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  94.  
  95.     // #ifndef ONLINE_JUDGE
  96.     //     freopen("input.txt", "r", stdin);
  97.     //     freopen("output.txt", "w", stdout);
  98.     // #endif
  99.  
  100.     int t = 1;
  101.     // int test = 1;
  102.     // cin >> t;
  103.     while(t--) {
  104.       // cout << "Case #" << test++ << ": ";
  105.       solve();
  106.     }
  107.  
  108.     return 0;
  109. }
  110.  
  111. /* # The final answer is stored in res variable, which is passed by reference in every fⁿ call.
  112.      In the main() fⁿ res is initialised as res= LLONG_MIN;
  113.    # Time Complexity: O(n), since we must visit each node, where n are the #nodes in binary tree.
  114.    # Auxiliary Space Complexity: O(1)
  115.    # Space complexity of the internal call stack: O(h), where h is the height of the binary tree,
  116.                                                   h may be O(log(n)), if a balanced tree or
  117.                                                   h maye be O(n), otherwise.
  118. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement