Banana-Leopard/TASTY huggingface dataset:
/* C++ program to construct a binary tree from
   the given string */
#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;
    Node *left, *right;
};
/* Helper function that allocates a new node */
Node* newNode(int data)
{
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
/* This function is here just to test  */
void preOrder(Node* node)
{
    if (node == NULL)
        return;
    printf("%d ", node->data);
    preOrder(node->left);
    preOrder(node->right);
}
 
// function to return the index of close parenthesis
int findIndex(string str, int si, int ei)
{
    if (si > ei)
        return -1;
 
    // Inbuilt stack
    stack<char> s;
 
    for (int i = si; i <= ei; i++) {
 
        // if open parenthesis, push it
        if (str[i] == '(')
            s.push(str[i]);
 
        // if close parenthesis
        else if (str[i] == ')') {
            if (s.top() == '(') {
                s.pop();
 
                // if stack is empty, this is
                // the required index
                if (s.empty())
                    return i;
            }
        }
    }
    // if not found return -1
    return -1;
}
 
// function to construct tree from string
Node* treeFromString(string str, int si, int ei)
{
    // Base case
    if (si > ei)
        return NULL;
 
     
      int num = 0;
      // In case the number is having more than 1 digit
      while(si <= ei && str[si] >= '0' && str[si] <= '9')
    {
      num *= 10;
      num += (str[si] - '0');
      si++;
    }
   
      // new root
    Node* root = newNode(num);
    int index = -1;
 
    // if next char is '(' find the index of
    // its complement ')'
    if (si <= ei && str[si] == '(')
        index = findIndex(str, si, ei);
 
    // if index found
    if (index != -1) {
 
        // call for left subtree
        root->left = treeFromString(str, si + 1, index - 1);
 
        // call for right subtree
        root->right
            = treeFromString(str, index + 2, ei - 1);
    }
    return root;
}
 
// Driver Code
int main()
{
    string str = "4(2(3)(1))(6(5))";
    Node* root = treeFromString(str, 0, str.length() - 1);
    preOrder(root);
}

Time complexity: quadratic
Space complexity: linear
// A simple inorder traversal based
// program to convert a Binary Tree to DLL
#include <bits/stdc++.h>
using namespace std;
 
// A tree node
class node
{
    public:
    int data;
    node *left, *right;
};
 
// A utility function to create
// a new tree node
node *newNode(int data)
{
    node *Node = new node();
    Node->data = data;
    Node->left = Node->right = NULL;
    return(Node);
}
 
// Standard Inorder traversal of tree
void inorder(node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        cout << "\t" << root->data;
        inorder(root->right);
    }
}
 
// Changes left pointers to work as
// previous pointers in converted DLL
// The function simply does inorder
// traversal of Binary Tree and updates
// left pointer using previously visited node
void fixPrevPtr(node *root)
{
    static node *pre = NULL;
 
    if (root != NULL)
    {
        fixPrevPtr(root->left);
        root->left = pre;
        pre = root;
        fixPrevPtr(root->right);
    }
}
 
// Changes right pointers to work
// as next pointers in converted DLL
node *fixNextPtr(node *root)
{
    node *prev = NULL;
 
    // Find the right most node
    // in BT or last node in DLL
    while (root && root->right != NULL)
        root = root->right;
 
    // Start from the rightmost node,
    // traverse back using left pointers.
    // While traversing, change right pointer of nodes.
    while (root && root->left != NULL)
    {
        prev = root;
        root = root->left;
        root->right = prev;
    }
 
    // The leftmost node is head
    // of linked list, return it
    return (root);
}
 
// The main function that converts
// BST to DLL and returns head of DLL
node *BTToDLL(node *root)
{
    // Set the previous pointer
    fixPrevPtr(root);
 
    // Set the next pointer and return head of DLL
    return fixNextPtr(root);
}
 
// Traverses the DLL from left to right
void printList(node *root)
{
    while (root != NULL)
    {
        cout<<"\t"<<root->data;
        root = root->right;
    }
}
 
// Driver code
int main(void)
{
    // Let us create the tree
    // shown in above diagram
    node *root = newNode(10);
    root->left = newNode(12);
    root->right = newNode(15);
    root->left->left = newNode(25);
    root->left->right = newNode(30);
    root->right->left = newNode(36);
 
    cout<<"\n\t\tInorder Tree Traversal\n\n";
    inorder(root);
 
    node *head = BTToDLL(root);
 
    cout << "\n\n\t\tDLL Traversal\n\n";
    printList(head);
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: linear
// A C++ program for in-place conversion of Binary Tree to
// DLL
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data, and left and right pointers
 */
struct node {
    int data;
    node* left;
    node* right;
};
 
node * bToDLL(node *root)
{
    stack<pair<node*, int>> s;
    s.push({root, 0});
    vector<int> res;
    bool flag = true;
    node* head = NULL;
    node* prev = NULL;
    while(!s.empty()) {
        auto x = s.top();
        node* t = x.first;
        int state = x.second;
        s.pop();
        if(state == 3 or t == NULL) continue;
        s.push({t, state+1});
        if(state == 0) s.push({t->left, 0});
        else if(state == 1) {
            if(prev) prev->right = t;
            t->left = prev;
            prev = t;
            if(flag) {
                head = t;
                flag = false;
            }
        }
        else if(state == 2) s.push({t->right, 0});
    }
    return head;
}
 
/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
node* newNode(int data)
{
    node* new_node = new node;
    new_node->data = data;
    new_node->left = new_node->right = NULL;
    return (new_node);
}
 
/* Function to print nodes in a given doubly linked list */
void printList(node* node)
{
    while (node != NULL) {
        cout << node->data << " ";
        node = node->right;
    }
}
 
// Driver Code
int main()
{
    // Let us create the tree shown in above diagram
    node* root = newNode(10);
    root->left = newNode(12);
    root->right = newNode(15);
    root->left->left = newNode(25);
    root->left->right = newNode(30);
    root->right->left = newNode(36);
 
    // Convert to DLL
    node* head = bToDLL(root);
 
    // Print the converted list
    printList(head);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to convert a given Binary Tree to Doubly Linked List
#include <bits/stdc++.h>
  
// Structure for tree and linked list
struct Node {
    int data;
    Node *left, *right;
};
  
// Utility function for allocating node for Binary
// Tree.
Node* newNode(int data)
{
    Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return node;
}
  
// A simple recursive function to convert a given
// Binary tree to Doubly Linked List
// root    --> Root of Binary Tree
// head --> Pointer to head node of created doubly linked list
void BToDLL(Node* root, Node*& head)
{
    // Base cases
    if (root == NULL)
        return;
  
    // Recursively convert right subtree
    BToDLL(root->right, head);
  
    // insert root into DLL
    root->right = head;
  
    // Change left pointer of previous head
    if (head != NULL)
        head->left = root;
  
    // Change head of Doubly linked list
    head = root;
  
    // Recursively convert left subtree
    BToDLL(root->left, head);
}
  
// Utility function for printing double linked list.
void printList(Node* head)
{
    printf("Extracted Double Linked list is:\n");
    while (head) {
        printf("%d ", head->data);
        head = head->right;
    }
}
  
// Driver program to test above function
int main()
{
    /* Constructing below tree 
            5 
            / \ 
            3     6 
        / \     \ 
        1 4     8 
        / \     / \ 
        0 2     7 9 */
    Node* root = newNode(5);
    root->left = newNode(3);
    root->right = newNode(6);
    root->left->left = newNode(1);
    root->left->right = newNode(4);
    root->right->right = newNode(8);
    root->left->left->left = newNode(0);
    root->left->left->right = newNode(2);
    root->right->right->left = newNode(7);
    root->right->right->right = newNode(9);
  
    Node* head = NULL;
    BToDLL(root, head);
  
    printList(head);
  
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to convert a binary tree
// to its mirror
#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
        = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
/* Change a tree so that the roles of the left and
    right pointers are swapped at every node.
 
So the tree...
     4
    / \
   2   5
  / \
 3   1
 
is changed to...
     4
    / \
   5   2
      / \
     1   3
*/
void mirror(struct Node* node)
{
    if (node == NULL)
        return;
    else {
        struct Node* temp;
 
        /* do the subtrees */
        mirror(node->left);
        mirror(node->right);
 
        /* swap the pointers in this node */
        temp = node->left;
        node->left = node->right;
        node->right = temp;
    }
}
 
/* Helper function to print
Inorder traversal.*/
void inOrder(struct Node* node)
{
    if (node == NULL)
        return;
 
    inOrder(node->left);
    cout << node->data << " ";
    inOrder(node->right);
}
 
// Driver Code
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);
 
    /* Print inorder traversal of the input tree */
    cout << "Inorder traversal of the constructed"
         << " tree is" << endl;
    inOrder(root);
 
    /* Convert tree to its mirror */
    mirror(root);
 
    /* Print inorder traversal of the mirror tree */
    cout << "\nInorder traversal of the mirror tree"
         << " is \n";
    inOrder(root);
 
    return 0;
}
 
// This code is contributed by Akanksha Rai

Time complexity: linear
Space complexity: linear
// Iterative CPP program to convert a Binary
// Tree to its mirror
#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);
}
 
/* Change a tree so that the roles of the  left and
    right pointers are swapped at every node.
 So the tree...
       4
      / \
     2   5
    / \
   1   3
 
 is changed to...
       4
      / \
     5   2
        / \
       3   1
*/
void mirror(Node* root)
{
    if (root == NULL)
        return;
 
    queue<Node*> q;
    q.push(root);
 
    // Do BFS. While doing BFS, keep swapping
    // left and right children
    while (!q.empty()) {
        // pop top node from queue
        Node* curr = q.front();
        q.pop();
 
        // swap left child with right child
        swap(curr->left, curr->right);
 
        // push left and right children
        if (curr->left)
            q.push(curr->left);
        if (curr->right)
            q.push(curr->right);
    }
}
 
/* Helper function to print Inorder traversal.*/
void inOrder(struct Node* node)
{
    if (node == NULL)
        return;
    inOrder(node->left);
    cout << node->data << " ";
    inOrder(node->right);
}
 
/* Driver program to test mirror() */
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);
 
    /* Print inorder traversal of the input tree */
    cout << "Inorder traversal of the"
            " constructed tree is \n";
    inOrder(root);
 
    /* Convert tree to its mirror */
    mirror(root);
 
    /* Print inorder traversal of the mirror tree */
    cout << "\nInorder traversal of the "
            "mirror tree is \n";
    inOrder(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ Program to convert a Binary Tree
// to a Circular Doubly Linked List
#include <iostream>
using namespace std;
 
// To represents a node of a Binary Tree
struct Node {
    struct Node *left, *right;
    int data;
};
 
// A function that appends rightList at the end
// of leftList.
Node* concatenate(Node* leftList, Node* rightList)
{
    // If either of the list is empty
    // then return the other list
    if (leftList == NULL)
        return rightList;
    if (rightList == NULL)
        return leftList;
 
    // Store the last Node of left List
    Node* leftLast = leftList->left;
 
    // Store the last Node of right List
    Node* rightLast = rightList->left;
 
    // Connect the last node of Left List
    // with the first Node of the right List
    leftLast->right = rightList;
    rightList->left = leftLast;
 
    // Left of first node points to
    // the last node in the list
    leftList->left = rightLast;
 
    // Right of last node refers to the first
    // node of the List
    rightLast->right = leftList;
 
    return leftList;
}
 
// Function converts a tree to a circular Linked List
// and then returns the head of the Linked List
Node* bTreeToCList(Node* root)
{
    if (root == NULL)
        return NULL;
 
    // Recursively convert left and right subtrees
    Node* left = bTreeToCList(root->left);
    Node* right = bTreeToCList(root->right);
 
    // Make a circular linked list of single node
    // (or root). To do so, make the right and
    // left pointers of this node point to itself
    root->left = root->right = root;
 
    // Step 1 (concatenate the left list with the list
    //         with single node, i.e., current node)
    // Step 2 (concatenate the returned list with the
    //         right List)
    return concatenate(concatenate(left, root), right);
}
 
// Display Circular Link List
void displayCList(Node* head)
{
    cout << "Circular Linked List is :\n";
    Node* itr = head;
    do {
        cout << itr->data << " ";
        itr = itr->right;
    } while (head != itr);
    cout << "\n";
}
 
// Create a new Node and return its address
Node* newNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Driver Program to test above function
int main()
{
    Node* root = newNode(10);
    root->left = newNode(12);
    root->right = newNode(15);
    root->left->left = newNode(25);
    root->left->right = newNode(30);
    root->right->left = newNode(36);
 
    Node* head = bTreeToCList(root);
    displayCList(head);
 
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: logn
// A C++ program for in-place conversion of Binary Tree to
// CDLL
#include <iostream>
using namespace std;
 
/* A binary tree node has - data , left and right pointers
 */
struct Node {
    int data;
    Node* left;
    Node* right;
};
// A utility function that converts given binary tree to
// a doubly linked list
// root --> the root of the binary tree
// head --> head of the created doubly linked list
Node* BTree2DoublyLinkedList(Node* root, Node** head)
{
    // Base case
    if (root == NULL)
        return root;
 
    // Initialize previously visited node as NULL. This is
    // static so that the same value is accessible in all
    // recursive calls
    static Node* prev = NULL;
 
    // Recursively convert left subtree
    BTree2DoublyLinkedList(root->left, head);
 
    // Now convert this node
    if (prev == NULL)
        *head = root;
    else {
        root->left = prev;
        prev->right = root;
    }
    prev = root;
 
    // Finally convert right subtree
    BTree2DoublyLinkedList(root->right, head);
    return prev;
}
// A simple recursive function to convert a given Binary
// tree to Circular Doubly Linked List using a utility
// function root --> Root of Binary Tree tail --> Pointer to
// tail node of created circular doubly linked list
Node* BTree2CircularDoublyLinkedList(Node* root)
{
    Node* head = NULL;
    Node* tail = BTree2DoublyLinkedList(root, &head);
    // make the changes to convert a DLL to CDLL
    tail->right = head;
    head->left = tail;
    // return the head of the created CDLL
    return head;
}
 
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
Node* newNode(int data)
{
    Node* new_node = new Node;
    new_node->data = data;
    new_node->left = new_node->right = NULL;
    return (new_node);
}
 
/* Function to print nodes in a given circular doubly linked
 * list */
void printList(Node* head)
{
    if (head == NULL)
        return;
    Node* ptr = head;
    do {
        cout << ptr->data << " ";
        ptr = ptr->right;
    } while (ptr != head);
}
 
/* Driver program to test above functions*/
int main()
{
    // Let us create the tree shown in above diagram
    Node* root = newNode(10);
    root->left = newNode(12);
    root->right = newNode(15);
    root->left->left = newNode(25);
    root->left->right = newNode(30);
    root->right->left = newNode(36);
 
    // Convert to DLL
    Node* head = BTree2CircularDoublyLinkedList(root);
 
    // Print the converted list
    printList(head);
 
    return 0;
}
 
// This code was contributed by Abhijeet
// Kumar(abhijeet19403)

Time complexity: linear
Space complexity: logn
// C++ program to convert a ternary expression to
// a tree.
#include<bits/stdc++.h>
using namespace std;
  
// tree structure
struct Node
{
    char data;
    Node *left, *right;
};
  
// function create a new node
Node *newNode(char Data)
{
    Node *new_node = new Node;
    new_node->data = Data;
    new_node->left = new_node->right = NULL;
    return new_node;
}
  
// Function to convert Ternary Expression to a Binary
// Tree. It return the root of tree
// Notice that we pass index i by reference because we 
// want to skip the characters in the subtree
Node *convertExpression(string str, int & i)
{
    // store current character of expression_string 
    // [ 'a' to 'z'] 
    Node * root =newNode(str[i]);
  
    //If it was last character return
    //Base Case
    if(i==str.length()-1) return root;
  
    // Move ahead in str 
    i++;
    //If the next character is '?'.Then there will be subtree for the current node
    if(str[i]=='?')
    {
        //skip the '?'
        i++;
  
        // construct the left subtree
        // Notice after the below recursive call i will point to ':' 
        // just before the right child of current node since we pass i by reference
        root->left = convertExpression(str,i);
          
        //skip the ':' character
        i++;
  
        //construct the right subtree
        root->right = convertExpression(str,i);
        return root;
    }
    //If the next character is not '?' no subtree just return it
    else return root;
}
  
// function print tree
void printTree( Node *root)
{
    if (!root)
        return ;
    cout << root->data <<" ";
    printTree(root->left);
    printTree(root->right);
}
  
// Driver program to test above function
int main()
{
    string expression = "a?b?c:d:e";
    int i=0;
    Node *root = convertExpression(expression, i);
    printTree(root) ;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program for Minimum swap required
// to convert binary tree to binary search tree
#include<bits/stdc++.h>
using namespace std;
 
// Inorder Traversal of Binary Tree
void inorder(int a[], std::vector<int> &v,
                        int n, int index)
{
    // if index is greater or equal to vector size
    if(index >= n)
        return;
    inorder(a, v, n, 2 * index + 1);
     
    // push elements in vector
    v.push_back(a[index]);
    inorder(a, v, n, 2 * index + 2);
}
 
// Function to find minimum swaps to sort an array
int minSwaps(std::vector<int> &v)
{
    std::vector<pair<int,int> > t(v.size());
    int ans = 0;
    for(int i = 0; i < v.size(); i++)
        t[i].first = v[i], t[i].second = i;
     
    sort(t.begin(), t.end());
    for(int i = 0; i < t.size(); i++)
    {
        // second element is equal to i
        if(i == t[i].second)
            continue;
        else
        {
            // swapping of elements
            swap(t[i].first, t[t[i].second].first);
            swap(t[i].second, t[t[i].second].second);
        }
         
        // Second is not equal to i
        if(i != t[i].second)
            --i;
        ans++;
    }
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 5, 6, 7, 8, 9, 10, 11 };
    int n = sizeof(a) / sizeof(a[0]);
    std::vector<int> v;
    inorder(a, v, n, 0);
    cout << minSwaps(v) << endl;
}
 
// This code is contributed by code_freak

Time complexity: nlogn
Space complexity: linear
/* Program to check children sum property */
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data, left
child and right child */
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
/* returns 1 if children sum property holds
for the given node and both of its children*/
int isSumProperty(struct node* node)
{
    /* left_data is left child data and
       right_data is for right child data*/
    int sum = 0;
 
    /* If node is NULL or it's a leaf node
    then return true */
    if (node == NULL
        || (node->left == NULL && node->right == NULL))
        return 1;
    else {
        /* If left child is not present then 0
        is used as data of left child */
        if (node->left != NULL)
            sum += node->left->data;
 
        /* If right child is not present then 0
        is used as data of right child */
        if (node->right != NULL)
            sum += node->right->data;
 
        /* if the node and both of its children
        satisfy the property return 1 else 0*/
        return ((node->data == sum)
                && isSumProperty(node->left)
                && isSumProperty(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
        = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return (node);
}
 
// Driver Code
int main()
{
    struct node* root = newNode(10);
    root->left = newNode(8);
    root->right = newNode(2);
    root->left->left = newNode(3);
    root->left->right = newNode(5);
    root->right->right = newNode(2);
 
    // Function call
    if (isSumProperty(root))
        cout << "The given tree satisfies "
             << "the children sum property ";
    else
        cout << "The given tree does not satisfy "
             << "the children sum property ";
 
    getchar();
    return 0;
}
// This code is contributed by Akanksha Rai

Time complexity: linear
Space complexity: logn