Banana-Leopard/TASTY huggingface dataset:
/* C++ program to pairwise swap
leaf nodes from left to right */
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node
{
    int data;
    struct Node *left, *right;
};
 
// function to swap two Node
void Swap(Node **a, Node **b)
{
    Node * temp = *a;
    *a = *b;
    *b = temp;
}
 
// two pointers to keep track of
// first and second nodes in a pair
Node **firstPtr;
Node **secondPtr;
 
// function to pairwise swap leaf
// nodes from left to right
void pairwiseSwap(Node **root, int &count)
{
    // if node is null, return
    if (!(*root))
        return;
 
    // if node is leaf node, increment count
    if(!(*root)->left&&!(*root)->right)
    {
        // initialize second pointer
        // by current node
        secondPtr = root;
 
        // increment count
        count++;
 
        // if count is even, swap first
        // and second pointers
        if (count%2 == 0)
            Swap(firstPtr, secondPtr);
 
        else
 
            // if count is odd, initialize
            // first pointer by second pointer
            firstPtr = secondPtr;
    }
 
    // if left child exists, check for leaf
    // recursively
    if ((*root)->left)
        pairwiseSwap(&(*root)->left, count);
 
    // if right child exists, check for leaf
    // recursively
    if ((*root)->right)
        pairwiseSwap(&(*root)->right, count);
 
}
 
// Utility function to create a new tree node
Node* newNode(int data)
{
    Node *temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// function to print inorder traversal
// of binary tree
void printInorder(Node* node)
{
    if (node == NULL)
        return;
 
    /* first recur on left child */
    printInorder(node->left);
 
    /* then print the data of node */
    printf("%d ", node->data);
 
    /* now recur on right child */
    printInorder(node->right);
}
 
// Driver program to test above functions
int main()
{
    // Let us create binary tree shown in
    // above diagram
    Node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->right->left = newNode(5);
    root->right->right = newNode(8);
    root->right->left->left = newNode(6);
    root->right->left->right = newNode(7);
    root->right->right->left = newNode(9);
    root->right->right->right = newNode(10);
 
    // print inorder traversal before swapping
    cout << "Inorder traversal before swap:\n";
    printInorder(root);
    cout << "\n";
 
    // variable to keep track
    // of leafs traversed
    int c = 0;
 
    // Pairwise swap of leaf nodes
    pairwiseSwap(&root, c);
 
    // print inorder traversal after swapping
    cout << "Inorder traversal after swap:\n";
    printInorder(root);
    cout << "\n";
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find the length of longest
// path with same values 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 val;
  struct Node *left, *right;
};
 
/* Function to print the longest path
   of same values */
int length(Node *node, int *ans) {
  if (!node)
    return 0;
 
  // Recursive calls to check for subtrees
  int left = length(node->left, ans);
  int right = length(node->right, ans);
 
  // Variables to store maximum lengths in two directions
  int Leftmax = 0, Rightmax = 0;
 
  // If curr node and it's left child has same value
  if (node->left && node->left->val == node->val)
    Leftmax += left + 1; 
 
  // If curr node and it's right child has same value
  if (node->right && node->right->val == node->val)
    Rightmax += right + 1;
   
  *ans = max(*ans, Leftmax + Rightmax);
  return max(Leftmax, Rightmax);
}
 
/* Driver function to find length of
   longest same value path*/
int longestSameValuePath(Node *root) {
  int ans = 0;
  length(root, &ans);
  return ans;
}
 
/* Helper function that allocates a
new node with the given data and
NULL left and right pointers. */
Node *newNode(int data) {
  Node *temp = new Node;
  temp->val = data;
  temp->left = temp->right = NULL;
  return temp;
}
 
// Driver code
int main() {
  /* Let us construct a Binary Tree
        4
       / \
      4   4
     / \   \
    4   9   5 */
 
  Node *root = NULL;
  root = newNode(4);
  root->left = newNode(4);
  root->right = newNode(4);
  root->left->left = newNode(4);
  root->left->right = newNode(9);
  root->right->right = newNode(5);
  cout << longestSameValuePath(root);
  return 0;
}

Time complexity: linear
Space complexity: logn
// C++ program to find distance of a given
// node from root.
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node
{
    int data;
    Node *left, *right;
};
 
// A utility function to create a new Binary
// Tree Node
Node *newNode(int item)
{
    Node *temp = new Node;
    temp->data = item;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Returns -1 if x doesn't exist in tree. Else
// returns distance of x from root
int findDistance(Node *root, int x)
{
    // Base case
    if (root == NULL)
      return -1;
 
    // Initialize distance
    int dist = -1;
 
    // Check if x is present at root or in left
    // subtree or right subtree.
    if ((root->data == x) ||
        (dist = findDistance(root->left, x)) >= 0 ||
        (dist = findDistance(root->right, x)) >= 0)
        return dist + 1;
 
    return dist;
}
 
// Driver Program to test above functions
int main()
{
    Node *root = newNode(5);
    root->left = newNode(10);
    root->right = newNode(15);
    root->left->left = newNode(20);
    root->left->right = newNode(25);
    root->left->right->right = newNode(45);
    root->right->left = newNode(30);
    root->right->right = newNode(35);
 
    cout << findDistance(root, 45);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C program to print right sibling of a node
#include <bits/stdc++.h>
 
// A Binary Tree Node
struct Node {
    int data;
    Node *left, *right, *parent;
};
 
// A utility function to create a new Binary
// Tree Node
Node* newNode(int item, Node* parent)
{
    Node* temp = new Node;
    temp->data = item;
    temp->left = temp->right = NULL;
    temp->parent = parent;
    return temp;
}
 
// Method to find right sibling
Node* findRightSibling(Node* node, int level)
{
    if (node == NULL || node->parent == NULL)
        return NULL;
 
    // GET Parent pointer whose right child is not
    // a parent or itself of this node. There might
    // be case when parent has no right child, but,
    // current node is left child of the parent
    // (second condition is for that).
    while (node->parent->right == node
           || (node->parent->right == NULL
               && node->parent->left == node)) {
        if (node->parent == NULL
            || node->parent->parent == NULL)
            return NULL;
 
        node = node->parent;
        level--;
    }
 
    // Move to the required child, where right sibling
    // can be present
    node = node->parent->right;
 
    if (node == NULL)
        return NULL;
    // find right sibling in the given subtree(from current
    // node), when level will be 0
    while (level < 0) {
 
        // Iterate through subtree
        if (node->left != NULL)
            node = node->left;
        else if (node->right != NULL)
            node = node->right;
        else
 
            // if no child are there, we cannot have right
            // sibling in this path
            break;
 
        level++;
    }
 
    if (level == 0)
        return node;
 
    // This is the case when we reach 9 node in the tree,
    // where we need to again recursively find the right
    // sibling
    return findRightSibling(node, level);
}
 
// Driver Program to test above functions
int main()
{
    Node* root = newNode(1, NULL);
    root->left = newNode(2, root);
    root->right = newNode(3, root);
    root->left->left = newNode(4, root->left);
    root->left->right = newNode(6, root->left);
    root->left->left->left = newNode(7, root->left->left);
    root->left->left->left->left = newNode(10, root->left->left->left);
    root->left->right->right = newNode(9, root->left->right);
    root->right->right = newNode(5, root->right);
    root->right->right->right = newNode(8, root->right->right);
    root->right->right->right->right = newNode(12, root->right->right->right);
 
    // passing 10
    Node* res = findRightSibling(root->left->left->left->left, 0);
    if (res == NULL)
        printf("No right sibling");
    else
        printf("%d", res->data);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
/* C++ program to find next right of a given key
   using preorder traversal */
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node {
    struct Node *left, *right;
    int key;
};
 
// Utility function to create a new tree node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Function to find next node for given node
// in same level in a binary tree by using
// pre-order traversal
Node* nextRightNode(Node* root, int k, int level,
                               int& value_level)
{
    // return null if tree is empty
    if (root == NULL)
        return NULL;
 
    // if desired node is found, set value_level
    // to current level
    if (root->key == k) {
        value_level = level;
        return NULL;
    }
 
    // if value_level is already set, then current
    // node is the next right node
    else if (value_level) {
        if (level == value_level)
            return root;
    }
 
    // recurse for left subtree by increasing level by 1
    Node* leftNode = nextRightNode(root->left, k,
                        level + 1,  value_level);
 
    // if node is found in left subtree, return it
    if (leftNode)
        return leftNode;
 
    // recurse for right subtree by increasing level by 1
    return nextRightNode(root->right, k, level + 1,
                                       value_level);
}
 
// Function to find next node of given node in the
//  same level in given binary tree
Node* nextRightNodeUtil(Node* root, int k)
{
    int value_level = 0;
 
    return nextRightNode(root, k, 1, value_level);
}
 
// A utility function to test above functions
void test(Node* root, int k)
{
    Node* nr = nextRightNodeUtil(root, k);
    if (nr != NULL)
        cout << "Next Right of " << k << " is "
             << nr->key << endl;
    else
        cout << "No next right node found for "
             << k << endl;
}
 
// Driver program to test above functions
int main()
{
    // Let us create binary tree given in the
    // above example
    Node* root = newNode(10);
    root->left = newNode(2);
    root->right = newNode(6);
    root->right->right = newNode(5);
    root->left->left = newNode(8);
    root->left->right = newNode(4);
 
    test(root, 10);
    test(root, 2);
    test(root, 6);
    test(root, 5);
    test(root, 8);
    test(root, 4);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP Program to find Tilt of 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 val;
    struct Node *left, *right;
};
 
/* Recursive function to calculate Tilt of
  whole tree */
int traverse(Node* root, int* tilt)
{
    if (!root)
        return 0;
 
    // Compute tilts of left and right subtrees
    // and find sums of left and right subtrees
    int left = traverse(root->left, tilt);
    int right = traverse(root->right, tilt);
 
    // Add current tilt to overall
    *tilt += abs(left - right);
 
    // Returns sum of nodes under current tree
    return left + right + root->val;
}
 
/* Driver function to print Tilt of whole tree */
int Tilt(Node* root)
{
    int tilt = 0;
    traverse(root, &tilt);
    return tilt;
}
 
/* Helper function that allocates a
new node with the given data and
NULL left and right pointers. */
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->val = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Driver code
int main()
{
    /* Let us construct a Binary Tree
        4
       / \
      2   9
     / \   \
    3   5   7 */
 
    Node* root = NULL;
    root = newNode(4);
    root->left = newNode(2);
    root->right = newNode(9);
    root->left->left = newNode(3);
    root->left->right = newNode(8);
    root->right->right = newNode(7);
    cout << "The Tilt of whole tree is " << Tilt(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find averages of all levels
// 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, *right;
};
 
string inorder(Node* node, unordered_map<string, int>& m)
{
    if (!node)
        return "";
 
    string str = "(";
    str += inorder(node->left, m);
    str += to_string(node->data);
    str += inorder(node->right, m);
    str += ")";
 
    // Subtree already present (Note that we use
    // unordered_map instead of unordered_set
    // because we want to print multiple duplicates
    // only once, consider example of 4 in above
    // subtree, it should be printed only once.
    if (m[str] == 1)
        cout << node->data << " ";
 
    m[str]++;
 
    return str;
}
 
// Wrapper over inorder()
void printAllDups(Node* root)
{
    unordered_map<string, int> m;
    inorder(root, m);
}
 
/* Helper function that allocates a
new node with the given data and
NULL left and right pointers. */
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Driver code
int main()
{
    Node* root = NULL;
    root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->right->left = newNode(2);
    root->right->left->left = newNode(4);
    root->right->right = newNode(4);
    printAllDups(root);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// CPP program to find largest three elements in
// a binary tree.
#include <bits/stdc++.h>
using namespace std;
 
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 = NULL;
  node->right = NULL;
  return (node);
}
 
// function to find three largest element
void threelargest(Node *root, int &first, int &second,
                                          int &third) {
  if (root == NULL)
    return;
 
  // if data is greater than first large number
  // update the top three list
  if (root->data > first) {
    third = second;
    second = first;
    first = root->data;
  }
 
  // if data is greater than second large number
  // and not equal to first update the bottom
  // two list
  else if (root->data > second && root->data != first) {
    third = second;
    second = root->data;
  }
 
  // if data is greater than third large number
  // and not equal to first & second update the
  // third highest list
  else if (root->data > third &&
           root->data != first &&
           root->data != second)
    third = root->data;
 
  threelargest(root->left, first, second, third);
  threelargest(root->right, first, second, third);
}
 
// driver function
int main() {
  struct Node *root = newNode(1);
  root->left = newNode(2);
  root->right = newNode(3);
  root->left->left = newNode(4);
  root->left->right = newNode(5);
  root->right->left = newNode(4);
  root->right->right = newNode(5);
 
  int first = 0, second = 0, third = 0;
  threelargest(root, first, second, third);
  cout << "three largest elements are "
       << first << " " << second << " "
       << third;
  return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print DFS traversal from
// a given vertex in a  given graph
#include <bits/stdc++.h>
using namespace std;
 
// Graph class represents a directed graph
// using adjacency list representation
class Graph {
public:
    map<int, bool> visited;
    map<int, list<int> > adj;
 
    // function to add an edge to graph
    void addEdge(int v, int w);
 
    // DFS traversal of the vertices
    // reachable from v
    void DFS(int v);
};
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}
 
void Graph::DFS(int v)
{
    // Mark the current node as visited and
    // print it
    visited[v] = true;
    cout << v << " ";
 
    // Recur for all the vertices adjacent
    // to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFS(*i);
}
 
// Driver's code
int main()
{
    // Create a graph given in the above diagram
    Graph g;
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3);
 
    cout << "Following is Depth First Traversal"
            " (starting from vertex 2) \n";
 
    // Function call
    g.DFS(2);
 
    return 0;
}
 
// improved by Vishnudev C

Time complexity: linear
Space complexity: linear
// C++ program to print DFS
// traversal for a given
// graph
#include <bits/stdc++.h>
using namespace std;
 
class Graph {
 
    // A function used by DFS
    void DFSUtil(int v);
 
public:
    map<int, bool> visited;
    map<int, list<int> > adj;
    // function to add an edge to graph
    void addEdge(int v, int w);
 
    // prints DFS traversal of the complete graph
    void DFS();
};
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}
 
void Graph::DFSUtil(int v)
{
    // Mark the current node as visited and print it
    visited[v] = true;
    cout << v << " ";
 
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i);
}
 
// The function to do DFS traversal. It uses recursive
// DFSUtil()
void Graph::DFS()
{
    // Call the recursive helper function to print DFS
    // traversal starting from all vertices one by one
    for (auto i : adj)
        if (visited[i.first] == false)
            DFSUtil(i.first);
}
 
// Driver's  Code
int main()
{
    // Create a graph given in the above diagram
    Graph g;
    g.addEdge(0, 1);
    g.addEdge(0, 9);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(9, 3);
 
    cout << "Following is Depth First Traversal \n";
       
      // Function call
    g.DFS();
 
    return 0;
}
// improved by Vishnudev C

Time complexity: linear
Space complexity: linear
// C++ program to find a mother vertex in O(V+E) time
#include <bits/stdc++.h>
using namespace std;
 
class Graph {
    int V; // No. of vertices
    list<int>* adj; // adjacency lists
 
    // A recursive function to print DFS starting from v
    void DFSUtil(int v, vector<bool>& visited);
 
public:
    Graph(int V);
    void addEdge(int v, int w);
    int findMother();
};
 
Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}
 
// A recursive function to print DFS starting from v
void Graph::DFSUtil(int v, vector<bool>& visited)
{
    // Mark the current node as visited and print it
    visited[v] = true;
 
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i, visited);
}
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}
 
// Returns a mother vertex if exists. Otherwise returns -1
int Graph::findMother()
{
    // visited[] is used for DFS. Initially all are
    // initialized as not visited
    vector<bool> visited(V, false);
 
    // To store last finished vertex (or mother vertex)
    int v = 0;
 
    // Do a DFS traversal and find the last finished
    // vertex
    for (int i = 0; i < V; i++) {
        if (visited[i] == false) {
            DFSUtil(i, visited);
            v = i;
        }
    }
 
    // If there exist mother vertex (or vertices) in given
    // graph, then v must be one (or one of them)
 
    // Now check if v is actually a mother vertex (or graph
    // has a mother vertex).  We basically check if every
    // vertex is reachable from v or not.
 
    // Reset all values in visited[] as false and do
    // DFS beginning from v to check if all vertices are
    // reachable from it or not.
    fill(visited.begin(), visited.end(), false);
    DFSUtil(v, visited);
    for (int i = 0; i < V; i++)
        if (visited[i] == false)
            return -1;
 
    return v;
}
 
// Driver code
int main()
{
    // Create a graph given in the above diagram
    Graph g(7);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 3);
    g.addEdge(4, 1);
    g.addEdge(6, 4);
    g.addEdge(5, 6);
    g.addEdge(5, 2);
    g.addEdge(6, 0);
 
      // Function call
    cout << "A mother vertex is " << g.findMother();
 
    return 0;
}

Time complexity: linear
Space complexity: linear
/*
 * C++ program to count all paths from a source to a
 * destination.
 * https://www.geeksforgeeks.org/count-possible-paths-two-vertices/
 * Note that the original example has been refactored.
 */
#include <bits/stdc++.h>
using namespace std;
 
/*
 * A directed graph using adjacency list representation;
 * every vertex holds a list of all neighbouring vertices
 * that can be reached from it.
 */
class Graph {
public:
    // Construct the graph given the number of vertices...
    Graph(int vertices);
    // Specify an edge between two vertices
    void add_edge(int src, int dst);
    // Call the recursive helper function to count all the
    // paths
    int count_paths(int src, int dst, int vertices);
 
private:
    int m_vertices;
    list<int>* m_neighbours;
    void path_counter(int src, int dst, int& path_count,
                      vector<bool>& visited);
};
 
Graph::Graph(int vertices)
{
    m_vertices = vertices; // unused!!
    /* An array of linked lists - each element corresponds
    to a vertex and will hold a list of neighbours...*/
    m_neighbours = new list<int>[vertices];
}
 
void Graph::add_edge(int src, int dst)
{
    m_neighbours[src].push_back(dst);
}
 
int Graph::count_paths(int src, int dst, int vertices)
{
    int path_count = 0;
    vector<bool> visited(vertices, false);
    path_counter(src, dst, path_count, visited);
    return path_count;
}
 
/*
 * A recursive function that counts all paths from src to
 * dst. Keep track of the count in the parameter.
 */
void Graph::path_counter(int src, int dst, int& path_count,
                         vector<bool>& visited)
{
    // If we've reached the destination, then increment
    // count...
    visited[src] = true;
    if (src == dst) {
        path_count++;
    }
    // ...otherwise recurse into all neighbours...
    else {
        for (auto neighbour : m_neighbours[src]) {
            if (!visited[neighbour])
                path_counter(neighbour, dst, path_count,
                             visited);
        }
    }
    visited[src] = false;
}
 
// Driver code
int main()
{
    // Create a graph given in the above diagram - see link
    Graph g(5);
    g.add_edge(0, 1);
    g.add_edge(0, 2);
    g.add_edge(0, 4);
    g.add_edge(1, 3);
    g.add_edge(1, 4);
    g.add_edge(2, 3);
    g.add_edge(2, 1);
    g.add_edge(3, 2);
     
      // Function call
    cout << g.count_paths(0, 4, 5);
 
    return 0;
}

Time complexity: np
Space complexity: linear