
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum Sum BST in Binary Tree in C++
Suppose we have a binary tree root, we have to find the maximum sum of all nodes of any subtree which is also a Binary Search Tree (BST).
So, if the input is like,
then the output will be 20, this is the sum of all nodes in the selected BST.
To solve this, we will follow these steps −
Create one block called Data, this will hold some members like sz, maxVal, minVal, ok, sum. Also define one initializer for data, that will take in this order (sz, minVal, maxVal, ok, and set sum as 0)
ret := 0
Define a method called solve(), this will take tree root
if not node is non-zero or val of node is same as 0, then −
return a new data with initializing it by (0, inf, -inf, true)
left := solve(left of node)
right = solve(right of node)
Create one Data type instance called curr
curr.ok := false
if node - > val >= right.minVal, then −
return curr
if node - > val <= left.maxVal, then −
return curr
if left.ok is non-zero and right.ok is non-zero, then −
curr.sum := val + left.sum + right.sum of node
ret := maximum of curr.sum and ret
curr.sz := 1 + left.sz + right.sz
curr.ok := true
curr.maxVal := maximum of node value and right.maxVal
curr.minVal := maximum of node value and left.minVal
return curr
From the main method do the following
ret := 0
solve(root)
return ret
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; }else{ q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; }else{ q.push(temp->right); } } } TreeNode *make_tree(vector<int> v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } struct Data{ int sz; int maxVal; int minVal; bool ok; int sum; Data(){} Data(int a, int b, int c, bool d){ sz = a; minVal = b; maxVal = c; ok = d; sum = 0; } }; class Solution { public: int ret = 0; Data solve(TreeNode* node){ if (!node || node->val == 0) return Data(0, INT_MAX, INT_MIN, true); Data left = solve(node->left); Data right = solve(node->right); Data curr; curr.ok = false; if (node->val >= right.minVal) { return curr; } if (node->val <= left.maxVal) { return curr; } if (left.ok && right.ok) { curr.sum = node->val + left.sum + right.sum; ret = max(curr.sum, ret); curr.sz = 1 + left.sz + right.sz; curr.ok = true; curr.maxVal = max(node->val, right.maxVal); curr.minVal = min(node->val, left.minVal); } return curr; } int maxSumBST(TreeNode* root){ ret = 0; solve(root); return ret; } }; main(){ Solution ob; vector<int> v = {1,4,3,2,4,2,5,NULL,NULL,NULL,NULL,NULL,NULL,4,6}; TreeNode *root = make_tree(v); cout << (ob.maxSumBST(root)); }
Input
{1,4,3,2,4,2,5,NULL,NULL,NULL,NULL,NULL,NULL,4,6}
Output
20
- Related Articles
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Binary Tree Maximum Path Sum in Python
- Maximum spiral sum in Binary Tree in C++
- Maximum parent children sum in Binary tree in C++
- Find maximum vertical sum in binary tree in C++
- Maximum Path Sum in a Binary Tree in C++
- Find maximum level sum in Binary Tree in C++
- Largest BST in a Binary Tree in C++
- Maximum Level Sum of a Binary Tree in C++
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++
- Maximum Binary Tree in C++
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++ program
- Maximum Binary Tree II in C++
- Program to find out the largest sum value of a BST in a given binary tree in Python
