Banana-Leopard/TASTY huggingface dataset:
// A recursive CPP program to find
// LCA of two nodes n1 and n2.
#include <bits/stdc++.h>
using namespace std;
 
class node {
public:
    int data;
    node *left, *right;
};
 
/* Function to find LCA of n1 and n2.
The function assumes that both
n1 and n2 are present in BST */
node* lca(node* root, int n1, int n2)
{
    if (root == NULL)
        return NULL;
 
    // If both n1 and n2 are smaller
    // than root, then LCA lies in left
    if (root->data > n1 && root->data > n2)
        return lca(root->left, n1, n2);
 
    // If both n1 and n2 are greater than
    // root, then LCA lies in right
    if (root->data < n1 && root->data < n2)
        return lca(root->right, n1, n2);
 
    return root;
}
 
/* Helper function that allocates
a new node with the given data.*/
node* newNode(int data)
{
    node* Node = new node();
    Node->data = data;
    Node->left = Node->right = NULL;
    return (Node);
}
 
/* Driver code*/
int main()
{
    // Let us construct the BST
    // shown in the above figure
    node* root = newNode(20);
    root->left = newNode(8);
    root->right = newNode(22);
    root->left->left = newNode(4);
    root->left->right = newNode(12);
    root->left->right->left = newNode(10);
    root->left->right->right = newNode(14);
 
      // Function calls
    int n1 = 10, n2 = 14;
    node* t = lca(root, n1, n2);
    cout << "LCA of " << n1 << " and " << n2 << " is "
         << t->data << endl;
 
    n1 = 14, n2 = 8;
    t = lca(root, n1, n2);
    cout << "LCA of " << n1 << " and " << n2 << " is "
         << t->data << endl;
 
    n1 = 10, n2 = 22;
    t = lca(root, n1, n2);
    cout << "LCA of " << n1 << " and " << n2 << " is "
         << t->data << endl;
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: constant
Space complexity: logn
// A recursive CPP program to find
// LCA of two nodes n1 and n2.
#include <bits/stdc++.h>
using namespace std;
 
class node {
public:
    int data;
    node *left, *right;
};
 
/* Function to find LCA of n1 and n2.
The function assumes that both n1 and n2
are present in BST */
node* lca(node* root, int n1, int n2)
{
    while (root != NULL) {
        // If both n1 and n2 are smaller than root,
        // then LCA lies in left
        if (root->data > n1 && root->data > n2)
            root = root->left;
 
        // If both n1 and n2 are greater than root,
        // then LCA lies in right
        else if (root->data < n1 && root->data < n2)
            root = root->right;
 
        else
            break;
    }
    return root;
}
 
/* Helper function that allocates
a new node with the given data.*/
node* newNode(int data)
{
    node* Node = new node();
    Node->data = data;
    Node->left = Node->right = NULL;
    return (Node);
}
 
/* Driver code*/
int main()
{
    // Let us construct the BST
    // shown in the above figure
    node* root = newNode(20);
    root->left = newNode(8);
    root->right = newNode(22);
    root->left->left = newNode(4);
    root->left->right = newNode(12);
    root->left->right->left = newNode(10);
    root->left->right->right = newNode(14);
 
      // Function calls
    int n1 = 10, n2 = 14;
    node* t = lca(root, n1, n2);
    cout << "LCA of " << n1 << " and " << n2 << " is "
         << t->data << endl;
 
    n1 = 14, n2 = 8;
    t = lca(root, n1, n2);
    cout << "LCA of " << n1 << " and " << n2 << " is "
         << t->data << endl;
 
    n1 = 10, n2 = 22;
    t = lca(root, n1, n2);
    cout << "LCA of " << n1 << " and " << n2 << " is "
         << t->data << endl;
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: constant
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data,
pointer to left child and
a pointer to right child */
class node {
public:
    int data;
    node* left;
    node* right;
 
    /* Constructor that allocates
    a new node with the given data
    and NULL left and right pointers. */
    node(int data)
    {
        this->data = data;
        this->left = NULL;
        this->right = NULL;
    }
};
 
int isBSTUtil(node* node, int min, int max);
 
/* Returns true if the given
tree is a binary search tree
(efficient version). */
int isBST(node* node)
{
    return (isBSTUtil(node, INT_MIN, INT_MAX));
}
 
/* Returns true if the given
tree is a BST and its values
are >= min and <= max. */
int isBSTUtil(node* node, int min, int max)
{
    /* an empty tree is BST */
    if (node == NULL)
        return 1;
 
    /* false if this node violates
    the min/max constraint */
    if (node->data < min || node->data > max)
        return 0;
 
    /* otherwise check the subtrees recursively,
    tightening the min or max constraint */
    return isBSTUtil(node->left, min, node->data - 1)
           && // Allow only distinct values
           isBSTUtil(node->right, node->data + 1,
                     max); // Allow only distinct values
}
 
/* Driver code*/
int main()
{
    node* root = new node(4);
    root->left = new node(2);
    root->right = new node(5);
    root->left->left = new node(1);
    root->left->right = new node(3);
 
      // Function call
    if (isBST(root))
        cout << "Is BST";
    else
        cout << "Not a BST";
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: constant
// C++ program to check if a given tree is BST.
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data, pointer to
left child and a pointer to right child */
struct Node {
    int data;
    struct Node *left, *right;
 
    Node(int data)
    {
        this->data = data;
        left = right = NULL;
    }
};
 
bool isBSTUtil(struct Node* root, Node*& prev)
{
    // traverse the tree in inorder fashion and
    // keep track of prev node
    if (root) {
        if (!isBSTUtil(root->left, prev))
            return false;
 
        // Allows only distinct valued nodes
        if (prev != NULL && root->data <= prev->data)
            return false;
 
        prev = root;
 
        return isBSTUtil(root->right, prev);
    }
 
    return true;
}
 
bool isBST(Node* root)
{
    Node* prev = NULL;
    return isBSTUtil(root, prev);
}
 
/* Driver code*/
int main()
{
    struct Node* root = new Node(3);
    root->left = new Node(2);
    root->right = new Node(5);
    root->left->left = new Node(1);
    root->left->right = new Node(4);
 
    // Function call
    if (isBST(root))
        cout << "Is BST";
    else
        cout << "Not a BST";
 
    return 0;
}

Time complexity: linear
Space complexity: logn
// A C++ program to check for Identical
// BSTs without building the trees
#include <bits/stdc++.h>
using namespace std;
 
/* The main function that checks if two
arrays a[] and b[] of size n construct
same BST. The two values 'min' and 'max'
decide whether the call is made for left
subtree or right subtree of a parent
element. The indexes i1 and i2 are the
indexes in (a[] and b[]) after which we
search the left or right child. Initially,
the call is made for INT_MIN and INT_MAX
as 'min' and 'max' respectively, because
root has no parent. i1 and i2 are just
after the indexes of the parent element in a[] and b[]. */
bool isSameBSTUtil(int a[], int b[], int n, int i1, int i2,
                   int min, int max)
{
    int j, k;
 
    /* Search for a value satisfying the
    constraints of min and max in a[] and
    b[]. If the parent element is a leaf
    node then there must be some elements
    in a[] and b[] satisfying constraint. */
    for (j = i1; j < n; j++)
        if (a[j] > min && a[j] < max)
            break;
    for (k = i2; k < n; k++)
        if (b[k] > min && b[k] < max)
            break;
 
    /* If the parent element is leaf in both arrays */
    if (j == n && k == n)
        return true;
 
    /* Return false if any of the following is true
        a) If the parent element is leaf in one array,
            but non-leaf in other.
        b) The elements satisfying constraints are
            not same. We either search for left
            child or right child of the parent
            element (decided by min and max values).
            The child found must be same in both arrays */
    if (((j == n) ^ (k == n)) || a[j] != b[k])
        return false;
 
    /* Make the current child as parent and
    recursively check for left and right
    subtrees of it. Note that we can also
    pass a[k] in place of a[j] as they
    are both are same */
    return isSameBSTUtil(a, b, n, j + 1, k + 1, a[j], max)
           && // Right Subtree
           isSameBSTUtil(a, b, n, j + 1, k + 1, min,
                         a[j]); // Left Subtree
}
 
// A wrapper over isSameBSTUtil()
bool isSameBST(int a[], int b[], int n)
{
    return isSameBSTUtil(a, b, n, 0, 0, INT_MIN, INT_MAX);
}
 
// Driver code
int main()
{
    int a[] = { 8, 3, 6, 1, 4, 7, 10, 14, 13 };
    int b[] = { 8, 10, 14, 3, 6, 4, 1, 7, 13 };
    int n = sizeof(a) / sizeof(a[0]);
    if (isSameBST(a, b, n)) {
        cout << "BSTs are same";
    }
    else {
        cout << "BSTs not same";
    }
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: linear
// CPP code for finding K-th largest Node using O(1)
// extra memory and reverse Morris traversal.
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    struct Node *left, *right;
};
 
// helper function to create a new Node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->right = temp->left = NULL;
    return temp;
}
 
Node* KthLargestUsingMorrisTraversal(Node* root, int k)
{
    Node* curr = root;
    Node* Klargest = NULL;
 
    // count variable to keep count of visited Nodes
    int count = 0;
 
    while (curr != NULL) {
        // if right child is NULL
        if (curr->right == NULL) {
 
            // first increment count and check if count = k
            if (++count == k)
                Klargest = curr;
 
            // otherwise move to the left child
            curr = curr->left;
        }
 
        else {
 
            // find inorder successor of current Node
            Node* succ = curr->right;
 
            while (succ->left != NULL && succ->left != curr)
                succ = succ->left;
 
            if (succ->left == NULL) {
 
                // set left child of successor to the
                // current Node
                succ->left = curr;
 
                // move current to its right
                curr = curr->right;
            }
 
            // restoring the tree back to original binary
            //  search tree removing threaded links
            else {
 
                succ->left = NULL;
 
                if (++count == k)
                    Klargest = curr;
 
                // move current to its left child
                curr = curr->left;
            }
        }
    }
 
    return Klargest;
}
 
int main()
{
    // Your C++ Code
    /* Constructed binary tree is
          4
        /   \
       2     7
     /  \   /  \
    1    3  6    10 */
 
    Node* root = newNode(4);
    root->left = newNode(2);
    root->right = newNode(7);
    root->left->left = newNode(1);
    root->left->right = newNode(3);
    root->right->left = newNode(6);
    root->right->right = newNode(10);
 
    cout << "Finding K-th largest Node in BST : "
         << KthLargestUsingMorrisTraversal(root, 2)->data;
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to check if a given array is sorted
// or not.
#include<bits/stdc++.h>
using namespace std;
 
// Function that returns true if array is Inorder
// traversal of any Binary Search Tree or not.
bool isInorder(int arr[], int n)
{
    // Array has one or no element
    if (n == 0 || n == 1)
        return true;
 
    for (int i = 1; i < n; i++)
 
        // Unsorted pair found
        if (arr[i-1] > arr[i])
            return false;
 
    // No unsorted pair found
    return true;
}
 
// Driver code
int main()
{
    int arr[] = { 19, 23, 25, 30, 45 };
    int n = sizeof(arr)/sizeof(arr[0]);
     
    if (isInorder(arr, n))
        cout << "Yesn";
    else
        cout << "Non";
         
  return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to check if two BSTs contains
// same set of elements
#include<bits/stdc++.h>
using namespace std;
 
// BST Node
struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
};
 
// Utility function to create new Node
Node* newNode(int val)
{
    Node* temp = new Node;
    temp->data = val;
    temp->left = temp->right = NULL;
    return temp;
}
 
// function to insert elements of the
// tree to map m
void insertToHash(Node* root, unordered_set<int> &s)
{
    if (!root)
        return;
    insertToHash(root->left, s);
    s.insert(root->data);
    insertToHash(root->right, s);
}
 
// function to check if the two BSTs contain
// same set  of elements
bool checkBSTs(Node* root1, Node* root2)
{
    // Base cases
    if (!root1 && !root2)
        return true;
    if ((root1 && !root2) || (!root1 && root2))
        return false;
         
    // Create two hash sets and store
    // elements both BSTs in them.
    unordered_set<int> s1, s2;
    insertToHash(root1, s1);
    insertToHash(root2, s2);
 
    // Return true if both hash sets
    // contain same elements.
    return (s1 == s2);
}
 
// Driver program to check above functions
int main()
{
    // First BST
    Node* root1 = newNode(15);
    root1->left = newNode(10);
    root1->right = newNode(20);
    root1->left->left = newNode(5);
    root1->left->right = newNode(12);
    root1->right->right = newNode(25);
     
    // Second BST
    Node* root2 = newNode(15);
    root2->left = newNode(12);
    root2->right = newNode(20);
    root2->left->left = newNode(5);
    root2->left->left->right = newNode(10);
    root2->right->right = newNode(25);
     
    // check if two BSTs have same set of elements
    if (checkBSTs(root1, root2))
        cout << "YES";
    else
        cout << "NO";
    return 0;       
}

Time complexity: linear
Space complexity: linear
// CPP program to check if two BSTs contains
// same set of elements
#include<bits/stdc++.h>
using namespace std;
 
// BST Node
struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
};
 
// Utility function to create new Node
Node* newNode(int val)
{
    Node* temp = new Node;
    temp->data = val;
    temp->left = temp->right = NULL;
    return temp;
}
 
// function to insert elements of the
// tree to map m
void storeInorder(Node* root, vector<int> &v)
{
    if (!root)
        return;
    storeInorder(root->left, v);
    v.push_back(root->data);
    storeInorder(root->right, v);
}
 
// function to check if the two BSTs contain
// same set  of elements
bool checkBSTs(Node* root1, Node* root2)
{
    // Base cases
    if (!root1 && !root2)
        return true;
    if ((root1 && !root2) || (!root1 && root2))
        return false;
         
    // Create two vectors and store
    // inorder traversals of both BSTs
    // in them.
    vector<int> v1, v2;
    storeInorder(root1, v1);
    storeInorder(root2, v2);
 
    // Return true if both vectors are
    // identical
    return (v1 == v2);
}
 
// Driver program to check above functions
int main()
{
    // First BST
    Node* root1 = newNode(15);
    root1->left = newNode(10);
    root1->right = newNode(20);
    root1->left->left = newNode(5);
    root1->left->right = newNode(12);
    root1->right->right = newNode(25);
     
    // Second BST
    Node* root2 = newNode(15);
    root2->left = newNode(12);
    root2->right = newNode(20);
    root2->left->left = newNode(5);
    root2->left->left->right = newNode(10);
    root2->right->right = newNode(25);
     
    // check if two BSTs have same set of elements
    if (checkBSTs(root1, root2))
        cout << "YES";
    else
        cout << "NO";
    return 0;       
}

Time complexity: linear
Space complexity: linear
// C++ program to find largest BST in a
// Binary Tree.
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};
 
/* Helper function that allocates a new
node with the given data and NULL left
and right pointers. */
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
 
    return (node);
}
 
// Information to be returned by every
// node in bottom up traversal.
struct Info {
    // Size of subtree
    int sz;
    // Min value in subtree
    int max;
    // Max value in subtree
    int min;
    // Size of largest BST which
    // is subtree of current node
    int ans;
    // If subtree is BST
    bool isBST;
};
 
// Returns Information about subtree. The
// Information also includes size of largest
// subtree which is a BST.
Info largestBSTBT(Node* root)
{
    // Base cases : When tree is empty or it has
    // one child.
    if (root == NULL)
        return { 0, INT_MIN, INT_MAX, 0, true };
    if (root->left == NULL && root->right == NULL)
        return { 1, root->data, root->data, 1, true };
 
    // Recur for left subtree and right subtrees
    Info l = largestBSTBT(root->left);
    Info r = largestBSTBT(root->right);
 
    // Create a return variable and initialize its
    // size.
    Info ret;
    ret.sz = (1 + l.sz + r.sz);
 
    // If whole tree rooted under current root is
    // BST.
    if (l.isBST && r.isBST && l.max < root->data
        && r.min > root->data) {
        ret.min = min(l.min, root->data);
        ret.max = max(r.max, root->data);
 
        // Update answer for tree rooted under
        // current 'root'
        ret.ans = l.ans + r.ans + 1;
        ret.isBST = true;
 
        return ret;
    }
 
    // If whole tree is not BST, return maximum
    // of left and right subtrees
    ret.ans = max(l.ans, r.ans);
    ret.isBST = false;
 
    return ret;
}
 
/* Driver program to test above functions*/
int main()
{
    /* Let us construct the following Tree
        60
       /  \
      65  70
     /
    50 */
 
    struct Node* root = newNode(60);
    root->left = newNode(65);
    root->right = newNode(70);
    root->left->left = newNode(50);
    printf(" Size of the largest BST is %d\n",
           largestBSTBT(root).ans);
    return 0;
}
 
// This code is contributed by Vivek Garg in a
// comment on below set 1.
// www.geeksforgeeks.org/find-the-largest-subtree-in-a-tree-that-is-also-a-bst/
// This code is updated by Naman Sharma

Time complexity: linear
Space complexity: linear
// A C++ program to remove BST keys
// outside the given range
 
#include<bits/stdc++.h>
using namespace std;
 
// A BST node has key, and left and right pointers
struct node
{
    int key;
    struct node *left;
    struct node *right;
};
 
// Removes all nodes having value outside the
// given range and returns the root of modified tree
node* removeOutsideRange(node *root, int min, int max)
{
   // Base Case
   if (root == NULL)
      return NULL;
 
   // First fix the left and right subtrees of root
   root->left =  removeOutsideRange(root->left, min, max);
   root->right =  removeOutsideRange(root->right, min, max);
 
   // Now fix the root.  There are 2 possible cases
   // for root 1.a) Root's key is smaller than min
   // value (root is not in range)
   if (root->key < min)
   {
       node *rChild = root->right;
       delete root;
       return rChild;
   }
   // 1.b) Root's key is greater than max value
   // (root is not in range)
   if (root->key > max)
   {
       node *lChild = root->left;
       delete root;
       return lChild;
   }
   // 2. Root is in range
   return root;
}
 
// A utility function to create a new BST
// node with key as given num
node* newNode(int num)
{
    node* temp = new node;
    temp->key = num;
    temp->left = temp->right = NULL;
    return temp;
}
 
// A utility function to insert a given key to BST
node* insert(node* root, int key)
{
    if (root == NULL)
       return newNode(key);
    if (root->key > key)
       root->left = insert(root->left, key);
    else
       root->right = insert(root->right, key);
    return root;
}
 
// Utility function to traverse the binary
// tree after conversion
void inorderTraversal(node* root)
{
    if (root)
    {
        inorderTraversal( root->left );
        cout << root->key << " ";
        inorderTraversal( root->right );
    }
}
 
// Driver program to test above functions
int main()
{
    node* root = NULL;
    root = insert(root, 6);
    root = insert(root, -13);
    root = insert(root, 14);
    root = insert(root, -8);
    root = insert(root, 15);
    root = insert(root, 13);
    root = insert(root, 7);
 
    cout << "Inorder traversal of the given tree is: ";
    inorderTraversal(root);
 
    root = removeOutsideRange(root, -10, 13);
 
    cout << "\nInorder traversal of the modified tree is: ";
    inorderTraversal(root);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print BST in given range
#include<bits/stdc++.h>
using namespace std;
 
/* A tree node structure */
class node
{
    public:
    int data;
    node *left;
    node *right;
};
 
/* The functions prints all the keys
which in the given range [k1..k2].
    The function assumes than k1 < k2 */
void Print(node *root, int k1, int k2)
{
    /* base case */
    if ( NULL == root )
        return;
     
    /* Since the desired o/p is sorted,
        recurse for left subtree first
        If root->data is greater than k1,
        then only we can get o/p keys
        in left subtree */
    if ( k1 < root->data )
        Print(root->left, k1, k2);
     
    /* if root's data lies in range,
    then prints root's data */
    if ( k1 <= root->data && k2 >= root->data )
        cout<<root->data<<" ";
     
    /* recursively call the right subtree */
   Print(root->right, k1, k2);
}
 
/* Utility function to create a new Binary Tree node */
node* newNode(int data)
{
    node *temp = new node();
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
     
    return temp;
}
 
/* Driver code */
int main()
{
    node *root = new node();
    int k1 = 10, k2 = 25;
     
    /* Constructing tree given
    in the above figure */
    root = newNode(20);
    root->left = newNode(8);
    root->right = newNode(22);
    root->left->left = newNode(4);
    root->left->right = newNode(12);
     
    Print(root, k1, k2);
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: logn