Banana-Leopard/TASTY huggingface dataset:
// C++ code to modify binary tree for
// traversal using only right pointer
#include <iostream>
#include <stack>
#include <stdio.h>
#include <stdlib.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;
};
 
// 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 struct Node;
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return (node);
}
 
// An iterative process to set the right
// pointer of Binary tree
void modifytree(struct Node* root)
{
    // Base Case
    if (root == NULL)
        return;
 
    // Create an empty stack and push root to it
    stack<Node*> nodeStack;
    nodeStack.push(root);
 
    /* Pop all items one by one.
        Do following for every popped item
       a) print it
       b) push its right child
       c) push its left child
    Note that right child is pushed first
    so that left is processed first */
    struct Node* pre = NULL;
    while (nodeStack.empty() == false) {
 
        // Pop the top item from stack
        struct Node* node = nodeStack.top();
 
        nodeStack.pop();
 
        // Push right and left children of
        // the popped node to stack
        if (node->right)
            nodeStack.push(node->right);
        if (node->left)
            nodeStack.push(node->left);
 
        // check if some previous node exists
        if (pre != NULL) {
 
            // set the right pointer of
            // previous node to current
            pre->right = node;
        }
 
        // set previous node as current node
        pre = node;
    }
}
 
// printing using right pointer only
void printpre(struct Node* root)
{
    while (root != NULL) {
        cout << root->data << " ";
        root = root->right;
    }
}
 
// Driver code
int main()
{
    /* Constructed binary tree is
            10
          /   \
        8      2
      /  \   
    3     5 
  */
    struct Node* root = newNode(10);
    root->left = newNode(8);
    root->right = newNode(2);
    root->left->left = newNode(3);
    root->left->right = newNode(5);
 
    modifytree(root);
    printpre(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
/* C++ program to construct tree using
inorder and preorder traversals */
#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:
    char data;
    node* left;
    node* right;
};
 
/* Prototypes for utility functions */
int search(char arr[], int strt, int end, char value);
node* newNode(char data);
 
/* Recursive function to construct binary
of size len from Inorder traversal in[]
and Preorder traversal pre[]. Initial values
of inStrt and inEnd should be 0 and len -1.
The function doesn't do any error checking
for cases where inorder and preorder do not
form a tree */
node* buildTree(char in[], char pre[], int inStrt, int inEnd)
{
    static int preIndex = 0;
 
    if (inStrt > inEnd)
        return NULL;
 
    /* Pick current node from Preorder
    traversal using preIndex
    and increment preIndex */
    node* tNode = newNode(pre[preIndex++]);
 
    /* If this node has no children then return */
    if (inStrt == inEnd)
        return tNode;
 
    /* Else find the index of this node in Inorder traversal */
    int inIndex = search(in, inStrt, inEnd, tNode->data);
 
    /* Using index in Inorder traversal, construct left and
    right subtress */
    tNode->left = buildTree(in, pre, inStrt, inIndex - 1);
    tNode->right = buildTree(in, pre, inIndex + 1, inEnd);
 
    return tNode;
}
 
/* UTILITY FUNCTIONS */
/* Function to find index of value in arr[start...end]
The function assumes that value is present in in[] */
int search(char arr[], int strt, int end, char value)
{
    int i;
    for (i = strt; i <= end; i++)
    {
        if (arr[i] == value)
            return i;
    }
}
 
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
node* newNode(char data)
{
    node* Node = new node();
    Node->data = data;
    Node->left = NULL;
    Node->right = NULL;
 
    return (Node);
}
 
/* This function is here just to test buildTree() */
void printInorder(node* node)
{
    if (node == NULL)
        return;
 
    /* first recur on left child */
    printInorder(node->left);
 
    /* then print the data of node */
    cout<<node->data<<" ";
 
    /* now recur on right child */
    printInorder(node->right);
}
 
/* Driver code */
int main()
{
    char in[] = { 'D', 'B', 'E', 'A', 'F', 'C' };
    char pre[] = { 'A', 'B', 'D', 'E', 'C', 'F' };
    int len = sizeof(in) / sizeof(in[0]);
    node* root = buildTree(in, pre, 0, len - 1);
 
    /* Let us test the built tree by
    printing Inorder traversal */
    cout << "Inorder traversal of the constructed tree is \n";
    printInorder(root);
}
 
// This is code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: linear
/* C++ program to construct tree using inorder
   and preorder traversals */
#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 {
    char data;
    struct Node* left;
    struct Node* right;
};
 
struct Node* newNode(char data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
/* Recursive function to construct binary of size
len from Inorder traversal in[] and Preorder traversal
pre[]. Initial values of inStrt and inEnd should be
0 and len -1. The function doesn't do any error
checking for cases where inorder and preorder
do not form a tree */
struct Node* buildTree(char in[], char pre[], int inStrt,
                       int inEnd, unordered_map<char, int>& mp)
{
    static int preIndex = 0;
 
    if (inStrt > inEnd)
        return NULL;
 
    /* Pick current node from Preorder traversal using preIndex
    and increment preIndex */
    char curr = pre[preIndex++];
    struct Node* tNode = newNode(curr);
 
    /* If this node has no children then return */
    if (inStrt == inEnd)
        return tNode;
 
    /* Else find the index of this node in Inorder traversal */
    int inIndex = mp[curr];
 
    /* Using index in Inorder traversal, construct left and
    right subtress */
    tNode->left = buildTree(in, pre, inStrt, inIndex - 1, mp);
    tNode->right = buildTree(in, pre, inIndex + 1, inEnd, mp);
 
    return tNode;
}
 
// This function mainly creates an unordered_map, then
// calls buildTree()
struct Node* buldTreeWrap(char in[], char pre[], int len)
{
    // Store indexes of all items so that we
    // we can quickly find later
    unordered_map<char, int> mp;
    for (int i = 0; i < len; i++)
        mp[in[i]] = i;
 
    return buildTree(in, pre, 0, len - 1, mp);
}
 
/* This function is here just to test buildTree() */
void printInorder(struct Node* node)
{
    if (node == NULL)
        return;
    printInorder(node->left);
    printf("%c ", node->data);
    printInorder(node->right);
}
 
/* Driver program to test above functions */
int main()
{
    char in[] = { 'D', 'B', 'E', 'A', 'F', 'C' };
    char pre[] = { 'A', 'B', 'D', 'E', 'C', 'F' };
    int len = sizeof(in) / sizeof(in[0]);
 
    struct Node* root = buldTreeWrap(in, pre, len);
 
    /* Let us test the built tree by printing
      Inorder traversal */
    printf("Inorder traversal of the constructed tree is \n");
    printInorder(root);
}

Time complexity: linear
Space complexity: linear
// C++ program to construct a tree using
// inorder and preorder traversal
#include<bits/stdc++.h>
using namespace std;
 
class TreeNode
{
    public:
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) { val = x; }
};
 
set<TreeNode*> s;
stack<TreeNode*> st;
 
// Function to build tree using given traversal
TreeNode* buildTree(int preorder[], int inorder[],int n)
{
 
    TreeNode* root = NULL;
    for (int pre = 0, in = 0; pre < n;)
    {
 
        TreeNode* node = NULL;
        do
        {
            node = new TreeNode(preorder[pre]);
            if (root == NULL)
            {
                root = node;
            }
            if (st.size() > 0)
            {
                if (s.find(st.top()) != s.end())
                {
                    s.erase(st.top());
                    st.top()->right = node;
                    st.pop();
                }
                else
                {
                    st.top()->left = node;
                }
            }
            st.push(node);
        } while (preorder[pre++] != inorder[in] && pre < n);
 
        node = NULL;
        while (st.size() > 0 && in < n &&
                st.top()->val == inorder[in])
        {
            node = st.top();
            st.pop();
            in++;
        }
 
        if (node != NULL)
        {
            s.insert(node);
            st.push(node);
        }
    }
 
    return root;
}
 
// Function to print tree in Inorder
void printInorder(TreeNode* node)
{
    if (node == NULL)
        return;
 
    /* first recur on left child */
    printInorder(node->left);
 
    /* then print the data of node */
    cout << node->val << " ";
 
    /* now recur on right child */
    printInorder(node->right);
}
 
// Driver code
int main()
{
    int in[] = { 9, 8, 4, 2, 10, 5, 10, 1, 6, 3, 13, 12, 7 };
    int pre[] = { 1, 2, 4, 8, 9, 5, 10, 10, 3, 6, 7, 12, 13 };
    int len = sizeof(in)/sizeof(int);
 
    TreeNode *root = buildTree(pre, in, len);
 
    printInorder(root);
    return 0;
}
 
// This code is contributed by Arnab Kundu

Time complexity: linear
Space complexity: linear
// CPP program to construct binary 
// tree from given array in level
// order fashion Tree Node
#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);
}
  
// Function to insert nodes in level order
Node* insertLevelOrder(int arr[],
                       int i, int n)
{
      Node *root = nullptr;
    // Base case for recursion
    if (i < n)
    {
        root = newNode(arr[i]);
          
        // insert left child
        root->left = insertLevelOrder(arr,
                   2 * i + 1, n);
  
        // insert right child
        root->right = insertLevelOrder(arr,
                  2 * i + 2, n);
    }
    return root;
}
  
// Function to print tree nodes in
// InOrder fashion
void inOrder(Node* root)
{
    if (root != NULL)
    {
        inOrder(root->left);
        cout << root->data <<" ";
        inOrder(root->right);
    }
}
  
// Driver program to test above function
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };
    int n = sizeof(arr)/sizeof(arr[0]);
    Node* root = insertLevelOrder(arr, 0, n);
    inOrder(root);
}
  
// This code is contributed by Chhavi and improved by Thangaraj

Time complexity: linear
Space complexity: linear
// Program for construction of Full 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
 
class node {
public:
    int data;
    node* left;
    node* right;
};
 
// A utility function to create a node
node* newNode(int data)
{
    node* temp = new node();
 
    temp->data = data;
    temp->left = temp->right = NULL;
 
    return temp;
}
 
// A recursive function to construct Full from pre[] and
// post[]. preIndex is used to keep track of index in pre[].
// l is low index and h is high index for the current
// subarray in post[]
node* constructTreeUtil(int pre[], int post[],
                        int* preIndex, int l, int h,
                        int size)
{
    // Base case
    if (*preIndex >= size || l > h)
        return NULL;
 
    // The first node in preorder traversal is root. So take
    // the node at preIndex from preorder and make it root,
    // and increment preIndex
    node* root = newNode(pre[*preIndex]);
    ++*preIndex;
 
    // If the current subarray has only one element, no need
    // to recur
    if (l == h)
        return root;
 
    // Search the next element of pre[] in post[]
    int i;
    for (i = l; i <= h; ++i)
        if (pre[*preIndex] == post[i])
            break;
 
    // Use the index of element found in postorder to divide
    // postorder array in two parts. Left subtree and right
    // subtree
    if (i <= h) {
        root->left = constructTreeUtil(pre, post, preIndex,
                                       l, i, size);
        root->right = constructTreeUtil(pre, post, preIndex,
                                        i + 1, h - 1, size);
    }
 
    return root;
}
 
// The main function to construct Full Binary Tree from
// given preorder and postorder traversals. This function
// mainly uses constructTreeUtil()
node* constructTree(int pre[], int post[], int size)
{
    int preIndex = 0;
    return constructTreeUtil(pre, post, &preIndex, 0,
                             size - 1, size);
}
 
// A utility function to print inorder traversal of a Binary
// Tree
void printInorder(node* node)
{
    if (node == NULL)
        return;
    printInorder(node->left);
    cout << node->data << " ";
    printInorder(node->right);
}
 
// Driver program to test above functions
int main()
{
    int pre[] = { 1, 2, 4, 8, 9, 5, 3, 6, 7 };
    int post[] = { 8, 9, 4, 5, 2, 6, 7, 3, 1 };
    int size = sizeof(pre) / sizeof(pre[0]);
 
    node* root = constructTree(pre, post, size);
 
    cout << "Inorder traversal of the constructed tree: \n";
    printInorder(root);
 
    return 0;
}

Time complexity: constant
Space complexity: logn
// C++ program to construct full binary tree
// using its preorder traversal and preorder
// traversal of its mirror tree
 
#include<bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node
{
    int data;
    struct Node *left, *right;
};
 
// 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;
}
 
// A utility function to print inorder traversal
// of a Binary Tree
void printInorder(Node* node)
{
    if (node == NULL)
        return;
 
    printInorder(node->left);
    printf("%d ", node->data);
    printInorder(node->right);
}
 
// A recursive function to construct Full binary tree
//  from pre[] and preM[]. preIndex is used to keep
// track of index in pre[]. l is low index and h is high
//index for the current subarray in preM[]
Node* constructBinaryTreeUtil(int pre[], int preM[],
                    int &preIndex, int l,int h,int size)
{   
    // Base case
    if (preIndex >= size || l > h)
        return NULL;
 
    // The first node in preorder traversal is root.
    // So take the node at preIndex from preorder and
    // make it root, and increment preIndex
    Node* root = newNode(pre[preIndex]);
        ++(preIndex);
 
    // If the current subarray has only one element,
    // no need to recur
    if (l == h)
        return root;
     
    // Search the next element of pre[] in preM[]
    int i;
    for (i = l; i <= h; ++i)
        if (pre[preIndex] == preM[i])
            break;
 
    // construct left and right subtrees recursively   
    if (i <= h)
    {
        root->left = constructBinaryTreeUtil (pre, preM,
                                    preIndex, i, h, size);
        root->right = constructBinaryTreeUtil (pre, preM,
                                 preIndex, l+1, i-1, size);
    }
  
     // return root
    return root;   
}
 
// function to construct full binary tree
// using its preorder traversal and preorder
// traversal of its mirror tree
void constructBinaryTree(Node* root,int pre[],
                        int preMirror[], int size)
{
    int preIndex = 0;
    int preMIndex = 0;
 
    root =  constructBinaryTreeUtil(pre,preMirror,
                            preIndex,0,size-1,size);
 
    printInorder(root);
}
 
// Driver program to test above functions
int main()
{
    int preOrder[] = {1,2,4,5,3,6,7};
    int preOrderMirror[] = {1,3,7,6,2,5,4};
 
    int size = sizeof(preOrder)/sizeof(preOrder[0]);
 
    Node* root = new Node;
 
    constructBinaryTree(root,preOrder,preOrderMirror,size);
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// A program to construct Binary Tree from preorder traversal
#include<bits/stdc++.h>
 
// A binary tree node structure
struct node
{
    int data;
    struct node *left;
    struct node *right;
};
 
// Utility function to create a new Binary Tree node
struct node* newNode (int data)
{
    struct node *temp = new struct node;
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
 
/* A recursive function to create a Binary Tree from given pre[]
   preLN[] arrays. The function returns root of tree. index_ptr is used
   to update index values in recursive calls. index must be initially
   passed as 0 */
struct node *constructTreeUtil(int pre[], char preLN[], int *index_ptr, int n)
{
    int index = *index_ptr; // store the current value of index in pre[]
 
    // Base Case: All nodes are constructed
    if (index == n)
        return NULL;
 
    // Allocate memory for this node and increment index for
    // subsequent recursive calls
    struct node *temp = newNode ( pre[index] );
    (*index_ptr)++;
 
    // If this is an internal node, construct left and
    // right subtrees and link the subtrees
    if (preLN[index] == 'N')
    {
      temp->left  = constructTreeUtil(pre, preLN, index_ptr, n);
      temp->right = constructTreeUtil(pre, preLN, index_ptr, n);
    }
 
    return temp;
}
 
// A wrapper over constructTreeUtil()
struct node *constructTree(int pre[], char preLN[], int n)
{
    /* Initialize index as 0. Value of index is
       used in recursion to maintain the current
       index in pre[] and preLN[] arrays. */
    int index = 0;
 
    return constructTreeUtil (pre, preLN, &index, n);
}
 
 
// This function is used only for testing
void printInorder (struct node* node)
{
    if (node == NULL)
        return;
 
    // First recur on left child
    printInorder (node->left);
 
    // Print the data of node
    printf("%d ", node->data);
 
    // Now recur on right child
    printInorder (node->right);
}
 
// Driver code
int main()
{
    struct node *root = NULL;
 
    /* Constructing tree given in the above figure
          10
         /  \
        30   15
       /  \
      20   5 */
    int pre[] = {10, 30, 20, 5, 15};
    char preLN[] = {'N', 'N', 'L', 'L', 'L'};
    int n = sizeof(pre)/sizeof(pre[0]);
 
    // construct the above tree
    root = constructTree (pre, preLN, n);
 
    // Test the constructed tree
    printf("Inorder Traversal of the Constructed Binary Tree: \n");
    printInorder (root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// A program to construct Binary Tree from preorder
// traversal
#include <bits/stdc++.h>
using namespace std;
 
// A binary tree node structure
struct node {
    int data;
    struct node* left;
    struct node* right;
    node(int x)
    {
        data = x;
        left = right = NULL;
    }
};
 
struct node* constructTree(int pre[], char preLN[], int n)
{
    // Taking an empty Stack
    stack<node*> st;
 
    // Setting up root node
    node* root = new node(pre[0]);
    // Checking if root is not leaf node
    if (preLN[0] != 'L')
        st.push(root);
 
    // Iterating over the given node values
    for (int i = 1; i < n; i++) {
        node* temp = new node(pre[i]);
 
        // Checking if the left position is NULL or not
        if (!st.top()->left) {
            st.top()->left = temp;
 
            // Checking for leaf node
            if (preLN[i] != 'L')
                st.push(temp);
        }
 
        // Checking if the right position is NULL or not
        else if (!st.top()->right) {
            st.top()->right = temp;
            if (preLN[i] != 'L')
 
                // Checking for leaf node
                st.push(temp);
        }
 
        // If left and right of top node is already filles
        else {
            while (st.top()->left && st.top()->right)
                st.pop();
            st.top()->right = temp;
 
            // Checking for leaf node
            if (preLN[i] != 'L')
                st.push(temp);
        }
    }
 
    // Returning the root of tree
    return root;
}
 
// This function is used only for testing
void printInorder(struct node* node)
{
    if (node == NULL)
        return;
 
    // First recur on left child
    printInorder(node->left);
 
    // Print the data of node
    printf("%d ", node->data);
 
    // Now recur on right child
    printInorder(node->right);
}
 
// Driver code
int main()
{
    struct node* root = NULL;
 
    /* Constructing tree given in the above figure
          10
         /  \
        30   15
       /  \
      20   5 */
    int pre[] = { 10, 30, 20, 5, 15 };
    char preLN[] = { 'N', 'N', 'L', 'L', 'L' };
    int n = sizeof(pre) / sizeof(pre[0]);
 
    // construct the above tree
    root = constructTree(pre, preLN, n);
 
    // Test the constructed tree
    printf("Inorder Traversal of the Constructed Binary Tree: \n");
    printInorder(root);
 
    return 0;
}
 
// This code is contributed by shubhamrajput6156

Time complexity: linear
Space complexity: linear