
- 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 sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
In this tutorial, we will be discussing a program to find maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST.
For this we will be provided with a binary tree. Our task is to print the sum of a sub-tree which is also a binary search tree.
Example
#include <bits/stdc++.h> using namespace std; //creating binary tree node struct Node { struct Node* left; struct Node* right; int data; Node(int data) { this->data = data; this->left = NULL; this->right = NULL; } }; struct Info { int max; int min; bool isBST; int sum; int currmax; }; Info MaxSumBSTUtil(struct Node* root, int& maxsum) { if (root == NULL) return { INT_MIN, INT_MAX, true, 0, 0 }; if (root->left == NULL && root->right == NULL) { maxsum = max(maxsum, root->data); return { root->data, root->data, true, root->data, maxsum }; } Info L = MaxSumBSTUtil(root->left, maxsum); Info R = MaxSumBSTUtil(root->right, maxsum); Info BST; if (L.isBST && R.isBST && L.max < root->data && R.min > root->data) { BST.max = max(root->data, max(L.max, R.max)); BST.min = min(root->data, min(L.min, R.min)); maxsum = max(maxsum, R.sum + root->data + L.sum); BST.sum = R.sum + root->data + L.sum; BST.currmax = maxsum; BST.isBST = true; return BST; } BST.isBST = false; BST.currmax = maxsum; BST.sum = R.sum + root->data + L.sum; return BST; } int MaxSumBST(struct Node* root) { int maxsum = INT_MIN; return MaxSumBSTUtil(root, maxsum).currmax; } int main() { struct Node* root = new Node(5); root->left = new Node(14); root->right = new Node(3); root->left->left = new Node(6); root->right->right = new Node(7); root->left->left->left = new Node(9); root->left->left->right = new Node(1); cout << MaxSumBST(root); return 0; }
Output
10
- Related Articles
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Maximum Sum BST in Binary Tree in C++
- C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
- Largest BST in a Binary Tree in C++
- Maximum Path Sum in a Binary Tree in C++
- Maximum Level Sum of a Binary Tree in C++
- Binary Tree Maximum Path Sum in Python
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Maximum spiral sum in Binary Tree in C++
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property in C++
- Maximum Binary Tree in C++
- Maximum parent children sum in Binary tree in C++
- Find maximum vertical sum in binary tree in C++
- Find maximum level sum in Binary Tree in C++

Advertisements