
- 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
Largest BST Subtree in C++
Suppose we have a binary tree; we have to find the largest subtree of it where largest means subtree with largest number of nodes in it.
So, if the input is like,
then the output will be 3, as the Largest BST Subtree, in this case, is the highlighted one.
To solve this, we will follow these steps −
Define one structure called data, there will be four values, size, maxVal, minVal, and ok, the ok can hold only true/false values
solve(TreeNode * node)
if node is null, then &miuns;
return Data by initializing (0, infinity, -infinity, true)
left := solve(left of node)
left := solve(right of node)
Define one data called curr
curr.ok := false
if val of node >= right.minVal, then −
return curr
if val of node <= left.maxVal, then −
return curr
if left.ok is true and right.ok is true, then −
curr.sz := 1 + left.sz + right.sz
curr.ok := true
curr.maxVal := maximum of (val of node and right.maxVal)
curr.minVal := maximum of (val of node and left.minVal)
if curr.ok is true, then −
ret := maximum of ret and curr.sz
return curr
From the main method, do the following −
ret := 0
solve(root)
return ret
Example
Let us see the following implementation to get a better understanding −
#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; Data(){} Data(int a, int b, int c, bool d){ sz = a; minVal = b; maxVal = c; ok = d; } }; class Solution { public: int ret; Data solve(TreeNode* node){ if (!node) 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.sz = 1 + left.sz + right.sz; curr.ok = true; curr.maxVal = max(node->val, right.maxVal); curr.minVal = min(node->val, left.minVal); } if (curr.ok) ret = max(ret, curr.sz); return curr; } int largestBSTSubtree(TreeNode* root){ ret = 0; solve(root); return ret; } }; main(){ Solution ob; vector<int< v = {10,5,15,1,8,NULL,7}; TreeNode *root= make_tree(v); cout << (ob.largestBSTSubtree(root)); }
Input
[10,5,15,1,8,null,7]
Output
3
- Related Articles
- Find the largest BST subtree in a given Binary Tree - Set 1 in C++
- Find largest subtree sum in a tree in C++
- Largest BST in a Binary Tree in C++
- Finding the second largest element in BST using C++
- Find the largest Complete Subtree in a given Binary Tree in C++
- Find largest subtree having identical left and right subtrees in Python
- Inverted Subtree in C++
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in Python
- C++ Largest Subtree having Equal No of 1's and 0's
- Program to find largest binary search subtree from a given tree in Python
- Convert a normal BST to Balanced BST in C++
- Most Frequent Subtree Sum in C++
- Find k-th smallest element in BST (Order Statistics in BST) in C++
- Inorder Successor in BST in C++
