Banana-Leopard/TASTY huggingface dataset:
// CPP code to print BST keys in given Range in
// constant space using Morris traversal.
#include <bits/stdc++.h>
 
using namespace std;
 
struct node {
 
    int data;
    struct node *left, *right;
};
 
// Function to print the keys in range
void RangeTraversal(node* root,
                    int n1, int n2)
{
    if (!root)
        return;
 
    node* curr = root;
 
    while (curr) {
 
        if (curr->left == NULL)
        {
            // check if current node
            // lies between n1 and n2
            if (curr->data <= n2 &&
                curr->data >= n1)
            {
                cout << curr->data << " ";
            }
 
            curr = curr->right;
        }
 
        else {
            node* pre = curr->left;
            // finding the inorder predecessor-
            // inorder predecessor is the right
            // most in left subtree or the left
            // child, i.e in BST it is the
            // maximum(right most) in left subtree.
            while (pre->right != NULL &&
                   pre->right != curr)
                        pre = pre->right;
 
            if (pre->right == NULL)
            {
                pre->right = curr;
                curr = curr->left;
            }
 
            else {
                pre->right = NULL;
 
                // check if current node lies
                // between n1 and n2
                if (curr->data <= n2 &&
                    curr->data >= n1)
                {
                    cout << curr->data << " ";
                }
 
                curr = curr->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;
}
 
// Driver Code
int main()
{
 
    /* 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);
 
    RangeTraversal(root, 4, 12);
     
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to count BST nodes within a given range
#include<bits/stdc++.h>
using namespace std;
 
// A BST node
struct node
{
    int data;
    struct node* left, *right;
};
 
// Utility function to create new node
node *newNode(int data)
{
    node *temp = new node;
    temp->data  = data;
    temp->left  = temp->right = NULL;
    return (temp);
}
 
// Returns count of nodes in BST in range [low, high]
int getCount(node *root, int low, int high)
{
    // Base case
    if (!root) return 0;
 
    // Special Optional case for improving efficiency
    if (root->data == high && root->data == low)
        return 1;
 
    // If current node is in range, then include it in count and
    // recur for left and right children of it
    if (root->data <= high && root->data >= low)
         return 1 + getCount(root->left, low, high) +
                    getCount(root->right, low, high);
 
    // If current node is smaller than low, then recur for right
    // child
    else if (root->data < low)
         return getCount(root->right, low, high);
 
    // Else recur for left child
    else return getCount(root->left, low, high);
}
 
// Driver program
int main()
{
    // Let us construct the BST shown in the above figure
    node *root        = newNode(10);
    root->left        = newNode(5);
    root->right       = newNode(50);
    root->left->left  = newNode(1);
    root->right->left = newNode(40);
    root->right->right = newNode(100);
    /* Let us constructed BST shown in above example
          10
        /    \
      5       50
     /       /  \
    1       40   100   */
    int l = 5;
    int h = 45;
    cout << "Count of nodes between [" << l << ", " << h
         << "] is " << getCount(root, l, h);
    return 0;
}

Time complexity: constant
Space complexity: linear
// c++ program to find Sum Of All Elements smaller
// than or equal to Kth Smallest Element In BST
#include <bits/stdc++.h>
using namespace std;
 
/* Binary tree Node */
struct Node
{
    int data;
    Node* left, * right;
};
 
// utility function new Node of BST
struct Node *createNode(int data)
{
    Node * new_Node = new Node;
    new_Node->left = NULL;
    new_Node->right = NULL;
    new_Node->data = data;
    return new_Node;
}
 
// A utility function to insert a new Node
//  with given key in BST and also maintain lcount ,Sum
struct Node * insert(Node *root, int key)
{
    // If the tree is empty, return a new Node
    if (root == NULL)
        return createNode(key);
 
    // Otherwise, recur down the tree
    if (root->data > key)
        root->left = insert(root->left, key);
 
    else if (root->data < key)
        root->right = insert(root->right, key);
 
    // return the (unchanged) Node pointer
    return root;
}
 
// function return sum of all element smaller than
// and equal to Kth smallest element
int ksmallestElementSumRec(Node *root, int k, int &count)
{
    // Base cases
    if (root == NULL)
        return 0;
    if (count > k)
        return 0;
 
    // Compute sum of elements in left subtree
    int res = ksmallestElementSumRec(root->left, k, count);
    if (count >= k)
        return res;
 
    // Add root's data
    res += root->data;
 
    // Add current Node
    count++;
    if (count >= k)
      return res;
 
    // If count is less than k, return right subtree Nodes
    return res + ksmallestElementSumRec(root->right, k, count);
}
 
// Wrapper over ksmallestElementSumRec()
int ksmallestElementSum(struct Node *root, int k)
{
   int count = 0;
   return ksmallestElementSumRec(root, k, count);
}
 
/* Driver program to test above functions */
int main()
{
 
    /*    20
        /    \
       8     22
     /   \
    4     12
         /   \
        10    14
          */
    Node *root = NULL;
    root = insert(root, 20);
    root = insert(root, 8);
    root = insert(root, 4);
    root = insert(root, 12);
    root = insert(root, 10);
    root = insert(root, 14);
    root = insert(root, 22);
 
    int k = 3;
    cout <<  ksmallestElementSum(root, k) <<endl;
    return 0;
}

Time complexity: constant
Space complexity: logn
// C++ program to find predecessor and successor in a BST
#include <iostream>
using namespace std;
 
// BST Node
struct Node
{
    int key;
    struct Node *left, *right;
};
 
// This function finds predecessor and successor of key in BST.
// It sets pre and suc as predecessor and successor respectively
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{
    // Base case
    if (root == NULL)  return ;
 
    // If key is present at root
    if (root->key == key)
    {
        // the maximum value in left subtree is predecessor
        if (root->left != NULL)
        {
            Node* tmp = root->left;
            while (tmp->right)
                tmp = tmp->right;
            pre = tmp ;
        }
 
        // the minimum value in right subtree is successor
        if (root->right != NULL)
        {
            Node* tmp = root->right ;
            while (tmp->left)
                tmp = tmp->left ;
            suc = tmp ;
        }
        return ;
    }
 
    // If key is smaller than root's key, go to left subtree
    if (root->key > key)
    {
        suc = root ;
        findPreSuc(root->left, pre, suc, key) ;
    }
    else // go to right subtree
    {
        pre = root ;
        findPreSuc(root->right, pre, suc, key) ;
    }
}
 
// A utility function to create a new BST node
Node *newNode(int item)
{
    Node *temp =  new Node;
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}
 
/* A utility function to insert a new node with given key in BST */
Node* insert(Node* node, int key)
{
    if (node == NULL) return newNode(key);
    if (key < node->key)
        node->left  = insert(node->left, key);
    else
        node->right = insert(node->right, key);
    return node;
}
 
// Driver program to test above function
int main()
{
    int key = 65;    //Key to be searched in BST
 
   /* Let us create following BST
              50
           /     \
          30      70
         /  \    /  \
       20   40  60   80 */
    Node *root = NULL;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);
 
 
    Node* pre = NULL, *suc = NULL;
 
    findPreSuc(root, pre, suc, key);
    if (pre != NULL)
      cout << "Predecessor is " << pre->key << endl;
    else
      cout << "No Predecessor";
 
    if (suc != NULL)
      cout << "Successor is " << suc->key;
    else
      cout << "No Successor";
    return 0;
}

Time complexity: logn
Space complexity: constant
// CPP code for inorder successor
// and predecessor of tree
#include<iostream>
#include<stdlib.h>
 
using namespace std;
 
struct Node
{
    int data;
    Node* left,*right;
};
  
// Function to return data
Node* getnode(int info)
{
    Node* p = (Node*)malloc(sizeof(Node));
    p->data = info;
    p->right = NULL;
    p->left = NULL;
    return p;
}
 
/*
since inorder traversal results in
ascending order visit to node , we
can store the values of the largest
no which is smaller than a (predecessor)
and smallest no which is large than
a (successor) using inorder traversal
*/
void find_p_s(Node* root,int a,
              Node** p, Node** q)
{
    // If root is null return
    if(!root)
        return ;
         
    // traverse the left subtree   
    find_p_s(root->left, a, p, q);
     
    // root data is greater than a
    if(root&&root->data > a)
    {
         
        // q stores the node whose data is greater
        // than a and is smaller than the previously
        // stored data in *q which is successor
        if((!*q) || (*q) && (*q)->data > root->data)
                *q = root;
    }
     
    // if the root data is smaller than
    // store it in p which is predecessor
    else if(root && root->data < a)
    {
        *p = root;
    }
     
    // traverse the right subtree
    find_p_s(root->right, a, p, q);
}
 
// Driver code
int main()
{
    Node* root1 = getnode(50);
    root1->left = getnode(20);
    root1->right = getnode(60);
    root1->left->left = getnode(10);
    root1->left->right = getnode(30);
    root1->right->left = getnode(55);
    root1->right->right = getnode(70);
    Node* p = NULL, *q = NULL;
  
    find_p_s(root1, 55, &p, &q);
     
    if(p)
        cout << p->data;
    if(q)
        cout << " " << q->data;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find predecessor
// and successor in a BST
#include <bits/stdc++.h>
using namespace std;
 
// BST Node
struct Node {
    int key;
    struct Node *left, *right;
};
 
// Function that finds predecessor and successor of key in BST.
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{
    if (root == NULL)
        return;
 
    // Search for given key in BST.
    while (root != NULL) {
 
        // If root is given key.
        if (root->key == key) {
 
            // the minimum value in right subtree
            // is successor.
            if (root->right) {
                suc = root->right;
                while (suc->left)
                    suc = suc->left;
            }
 
            // the maximum value in left subtree
            // is predecessor.
            if (root->left) {
                pre = root->left;
                while (pre->right)
                    pre = pre->right;
            }
 
            return;
        }
 
        // If key is greater than root, then
        // key lies in right subtree. Root
        // could be predecessor if left
        // subtree of key is null.
        else if (root->key < key) {
            pre = root;
            root = root->right;
        }
 
        // If key is smaller than root, then
        // key lies in left subtree. Root
        // could be successor if right
        // subtree of key is null.
        else {
            suc = root;
            root = root->left;
        }
    }
}
 
// A utility function to create a new BST node
Node* newNode(int item)
{
    Node* temp = new Node;
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}
 
// A utility function to insert
// a new node with given key in BST
Node* insert(Node* node, int key)
{
    if (node == NULL)
        return newNode(key);
    if (key < node->key)
        node->left = insert(node->left, key);
    else
        node->right = insert(node->right, key);
    return node;
}
 
// Driver program to test above function
int main()
{
    int key = 65; // Key to be searched in BST
 
    /* Let us create following BST
                 50
                /  \
               /    \
              30     70
             / \     / \
            /   \   /   \
           20   40 60   80
*/
    Node* root = NULL;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);
 
    Node *pre = NULL, *suc = NULL;
 
    findPreSuc(root, pre, suc, key);
    if (pre != NULL)
        cout << "Predecessor is " << pre->key << endl;
    else
        cout << "-1";
 
    if (suc != NULL)
        cout << "Successor is " << suc->key;
    else
        cout << "-1";
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find a pair with
// given sum using hashing
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    struct Node *left, *right;
};
 
Node* NewNode(int data)
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
 
Node* insert(Node* root, int key)
{
    if (root == NULL)
        return NewNode(key);
    if (key < root->data)
        root->left = insert(root->left, key);
    else
        root->right = insert(root->right, key);
    return root;
}
 
bool findpairUtil(Node* root, int sum,
                  unordered_set<int>& set)
{
    if (root == NULL)
        return false;
 
    if (findpairUtil(root->left, sum, set))
        return true;
 
    if (set.find(sum - root->data) != set.end()) {
        cout << "Pair is found (" << sum - root->data
             << ", " << root->data << ")" << endl;
        return true;
    }
    else
        set.insert(root->data);
 
    return findpairUtil(root->right, sum, set);
}
 
void findPair(Node* root, int sum)
{
    unordered_set<int> set;
    if (!findpairUtil(root, sum, set))
        cout << "Pairs do not exit" << endl;
}
 
// Driver code
int main()
{
    Node* root = NULL;
    root = insert(root, 15);
    root = insert(root, 10);
    root = insert(root, 20);
    root = insert(root, 8);
    root = insert(root, 12);
    root = insert(root, 16);
    root = insert(root, 25);
    root = insert(root, 10);
 
    int sum = 28;
    findPair(root, sum);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// Recursive C++ program to find key closest to k
// in given Binary Search Tree.
#include<bits/stdc++.h>
using namespace std;
 
/* A binary tree node has key, pointer to left child
and a pointer to right child */
struct Node
{
    int key;
    struct Node* left, *right;
};
 
/* Utility that allocates a new node with the
  given key and NULL left and right pointers. */
struct Node* newnode(int key)
{
    struct Node* node = new (struct Node);
    node->key = key;
    node->left = node->right  = NULL;
    return (node);
}
 
// Function to find node with minimum absolute
// difference with given K
// min_diff   --> minimum difference till now
// min_diff_key  --> node having minimum absolute
//                   difference with K
void maxDiffUtil(struct Node *ptr, int k, int &min_diff,
                                      int &min_diff_key)
{
    if (ptr == NULL)
        return ;
 
    // If k itself is present
    if (ptr->key == k)
    {
        min_diff_key = k;
        return;
    }
 
    // update min_diff and min_diff_key by checking
    // current node value
    if (min_diff > abs(ptr->key - k))
    {
        min_diff = abs(ptr->key - k);
        min_diff_key = ptr->key;
    }
 
    // if k is less than ptr->key then move in
    // left subtree else in right subtree
    if (k < ptr->key)
        maxDiffUtil(ptr->left, k, min_diff, min_diff_key);
    else
        maxDiffUtil(ptr->right, k, min_diff, min_diff_key);
}
 
// Wrapper over maxDiffUtil()
int maxDiff(Node *root, int k)
{
    // Initialize minimum difference
    int min_diff = INT_MAX, min_diff_key = -1;
 
    // Find value of min_diff_key (Closest key
    // in tree with k)
    maxDiffUtil(root, k, min_diff, min_diff_key);
 
    return min_diff_key;
}
 
// Driver program to run the case
int main()
{
    struct Node *root = newnode(9);
    root->left    = newnode(4);
    root->right   = newnode(17);
    root->left->left = newnode(3);
    root->left->right = newnode(6);
    root->left->right->left = newnode(5);
    root->left->right->right = newnode(7);
    root->right->right = newnode(22);
    root->right->right->left = newnode(20);
    int k = 18;
    cout << maxDiff(root, k);
    return 0;
}

Time complexity: constant
Space complexity: constant
// 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;
      Node(int val)
    {
          this->data = val;
          left = NULL;
          right = NULL;
    }
};
 
vector<int> largestBSTBT(Node* root)
{
    // Base cases : When tree is empty or it has one child.
    if (root == NULL)
        return {INT_MAX, INT_MIN, 0};
    if (root->left == NULL && root->right == NULL)
        return {root->data, root->data, 1};
 
    // Recur for left subtree and right subtrees
    vector<int> left = largestBSTBT(root->left);
    vector<int> right = largestBSTBT(root->right);
 
    // Create a return variable and initialize its size.
    vector<int> ans(3, 0);
 
    // If whole tree rooted under current root is BST.
    if ((left[1] < root->data) && (right[0] > root->data))
    {
        ans[0] = min(left[0], min(right[0], root->data));
        ans[1] = max(right[1], max(left[1], root->data));
        // Update answer for tree rooted under current 'root'
        ans[2] = 1 + left[2] + right[2];
        return ans;
    }
 
    // If whole tree is not BST, return maximum of left and right subtrees
    ans[0] = INT_MIN;
    ans[1] = INT_MAX;
    ans[2] = max(left[2], right[2]);
 
    return ans;
}
 
int largestBSTBTutil(Node *root)
{
      return largestBSTBT(root)[2];
}
 
// Driver Function
int main() {
 
    /* Let us construct the following Tree
        50
       /  \
      75  45
     /
    45 */
 
    struct Node *root = new Node(50);
    root->left = new Node(75);
    root->right = new Node(45);
    root->left->left = new Node(40);
    printf(" Size of the largest BST is %d\n", largestBSTBTutil(root));
    return 0;
}
 
// This Code is cuntributed by Ajay Makvana

Time complexity: linear
Space complexity: linear
// C++ program to add all greater
// values in every node of BST
#include <bits/stdc++.h>
using namespace std;
 
class Node {
public:
    int data;
    Node *left, *right;
};
 
// A utility function to create
// a new BST node
Node* newNode(int item)
{
    Node* temp = new Node();
    temp->data = item;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Recursive function to add all
// greater values in every node
void modifyBSTUtil(Node* root, int* sum)
{
    // Base Case
    if (root == NULL)
        return;
 
    // Recur for right subtree
    modifyBSTUtil(root->right, sum);
 
    // Now *sum has sum of nodes
    // in right subtree, add
    // root->data to sum and
    // update root->data
    *sum = *sum + root->data;
    root->data = *sum;
 
    // Recur for left subtree
    modifyBSTUtil(root->left, sum);
}
 
// A wrapper over modifyBSTUtil()
void modifyBST(Node* root)
{
    int sum = 0;
    modifyBSTUtil(root, ∑);
}
 
// A utility function to do
// inorder traversal of BST
void inorder(Node* root)
{
    if (root != NULL) {
        inorder(root->left);
        cout << root->data << " ";
        inorder(root->right);
    }
}
 
/* A utility function to insert
a new node with given data in BST */
Node* insert(Node* node, int data)
{
    /* If the tree is empty,
       return a new node */
    if (node == NULL)
        return newNode(data);
 
    /* Otherwise, recur down the tree */
    if (data <= node->data)
        node->left = insert(node->left, data);
    else
        node->right = insert(node->right, data);
 
    /* return the (unchanged) node pointer */
    return node;
}
 
// Driver code
int main()
{
    /* Let us create following BST
            50
        / \
        30 70
        / \ / \
    20 40 60 80 */
    Node* root = NULL;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);
 
    modifyBST(root);
 
    // print inorder traversal of the modified BST
    inorder(root);
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: constant