Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int maxSize = -1;
- pair< pair<bool,int>, pair<int,int> > largest_bst(TreeNode *cur){
- pair< pair<bool,int> , pair<int,int> > nodeValue; // first value is subtree size, second (first value is minimum in subtree, second value is maximum in subtree)
- nodeValue.first.first = true; // empty subtree is BST
- nodeValue.first.second = 0; // initialize subtree size
- nodeValue.second.first = INT_MAX; // initialize with maximum integer range
- nodeValue.second.second = INT_MIN; // initialize with minimum integer range
- if(cur == NULL)
- return nodeValue;
- pair< pair<bool,int>, pair<int,int> > left_child = largest_bst(cur->left);
- pair< pair<bool,int>, pair<int,int> > right_child = largest_bst(cur->right);
- nodeValue.first.first = left_child.first.first & right_child.first.first;
- if(nodeValue.first.first && cur->data > left_child.second.second && cur->data <= right_child.second.first){
- nodeValue.first.second = left_child.first + right_child.first + 1;
- nodeValue.second.first = min(left_child.second.first, cur->data);
- nodeValue.second.second = max(right_child.second.second, cur->data);
- maxSize = max(maxSize, nodeValue.first);
- return nodeValue;
- }
- return nodeValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement