Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. int maxSize = -1;
  2.  
  3. pair< pair<bool,int>, pair<int,int> > largest_bst(TreeNode *cur){
  4.     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)
  5.     nodeValue.first.first = true; // empty subtree is BST
  6.     nodeValue.first.second = 0; // initialize subtree size
  7.     nodeValue.second.first = INT_MAX; // initialize with maximum integer range
  8.     nodeValue.second.second = INT_MIN; // initialize with minimum integer range
  9.  
  10.     if(cur == NULL)
  11.         return  nodeValue;
  12.  
  13.     pair< pair<bool,int>, pair<int,int> > left_child = largest_bst(cur->left);
  14.     pair< pair<bool,int>, pair<int,int> > right_child = largest_bst(cur->right);
  15.  
  16.     nodeValue.first.first = left_child.first.first & right_child.first.first;
  17.  
  18.     if(nodeValue.first.first && cur->data > left_child.second.second && cur->data <= right_child.second.first){
  19.         nodeValue.first.second = left_child.first + right_child.first + 1;
  20.         nodeValue.second.first = min(left_child.second.first, cur->data);
  21.         nodeValue.second.second = max(right_child.second.second, cur->data);
  22.  
  23.         maxSize = max(maxSize, nodeValue.first);
  24.  
  25.         return nodeValue;
  26.     }
  27.  
  28.     return nodeValue;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement