Banana-Leopard/TASTY huggingface dataset:
// Given an ancestor matrix for binary tree, construct
// the tree.
#include <bits/stdc++.h>
using namespace std;
   
# define N 6
   
/* A binary tree node */
struct Node
{
    int data;
    Node *left, *right;
};
   
/* Helper function to create a new node */
Node* newNode(int data)
{
    Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
   
// Constructs tree from ancestor matrix
Node* ancestorTree(int mat[][N])
{
    // Binary array to determine whether
    // parent is set for node i or not
    int parent[N] = {0};
   
    // Root will store the root of the constructed tree
    Node* root = NULL;
   
    // Create a multimap, sum is used as key and row
    // numbers are used as values
    multimap<int, int> mm;
   
    for (int i = 0; i < N; i++)
    {
        int sum = 0; // Initialize sum of this row
        for (int j = 0; j < N; j++)
            sum += mat[i][j];
   
        // insert(sum, i) pairs into the multimap
        mm.insert(pair<int, int>(sum, i));
    }
   
    // node[i] will store node for i in constructed tree
    Node* node[N];
   
    // Traverse all entries of multimap.  Note that values
    // are accessed in increasing order of sum
    for (auto it = mm.begin(); it != mm.end(); ++it)
    {
      // create a new node for every value
      node[it->second] = newNode(it->second);
   
      // To store last processed node. This node will be
      // root after loop terminates
      root = node[it->second];
   
      // if non-leaf node
      if (it->first != 0)
      {
        // traverse row 'it->second' in the matrix
        for (int i = 0; i < N; i++)
        {
           // if parent is not set and ancestor exits
           if (!parent[i] && mat[it->second][i])
           {
             // check for unoccupied left/right node
             // and set parent of node i
             if (!node[it->second]->left)
               node[it->second]->left = node[i];
             else
               node[it->second]->right = node[i];
   
             parent[i] = 1;
           }
        }
      }
    }
    return root;
}
   
/* Given a binary tree, print its nodes in inorder */
void printInorder(Node* node)
{
    if (node == NULL)
        return;
    printInorder(node->left);
    printf("%d ", node->data);
    printInorder(node->right);
}
   
// Driver code
int main()
{
    int mat[N][N] = {{ 0, 0, 0, 0, 0, 0 },
        { 1, 0, 0, 0, 1, 0 },
        { 0, 0, 0, 1, 0, 0 },
        { 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0 },
        { 1, 1, 1, 1, 1, 0 }
    };
   
    Node* root = ancestorTree(mat);
   
    cout << "Inorder traversal of tree is \n";
     
    // Function call
    printInorder(root);
   
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to construct ancestor matrix for
// given tree.
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
 
/* A binary tree node */
struct Node
{
    int data;
    Node *left, *right;
};
 
// Creating a global boolean matrix for simplicity
bool mat[MAX][MAX];
 
// anc[] stores all ancestors of current node.  This
// function fills ancestors for all nodes.
// It also returns size of tree.  Size of tree is
// used to print ancestor matrix.
int ancestorMatrixRec(Node *root, vector<int> &anc)
{
    /* base case */
    if (root == NULL) return 0;;
 
    // Update all ancestors of current node
    int data = root->data;
    for (int i=0; i<anc.size(); i++)
       mat[anc[i]][data] = true;
 
    // Push data to list of ancestors
    anc.push_back(data);
 
    // Traverse left and right subtrees
    int l = ancestorMatrixRec(root->left, anc);
    int r = ancestorMatrixRec(root->right, anc);
 
    // Remove data from list the list of ancestors
    // as all descendants of it are processed now.
    anc.pop_back();
 
    return l+r+1;
}
 
// This function mainly calls ancestorMatrixRec()
void ancestorMatrix(Node *root)
{
    // Create an empty ancestor array
    vector<int> anc;
 
    // Fill ancestor matrix and find size of
    // tree.
    int n = ancestorMatrixRec(root, anc);
 
    // Print the filled values
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
            cout << mat[i][j] << " ";
        cout << endl;
    }
}
 
/* Helper function to create a new node */
Node* newnode(int data)
{
    Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
/* Driver program to test above functions*/
int main()
{
    /* Construct the following binary tree
                5
              /   \
            1      2
          /  \    /
         0    4  3    */
    Node *root        = newnode(5);
    root->left        = newnode(1);
    root->right       = newnode(2);
    root->left->left  = newnode(0);
    root->left->right = newnode(4);
    root->right->left = newnode(3);
 
    ancestorMatrix(root);
 
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to construct ancestor matrix for
// given tree.
#include<bits/stdc++.h>
using namespace std;
#define size 6
 
int M[size][size]={0};
 
/* A binary tree node */
struct Node
{
    int data;
    Node *left, *right;
};
 
/* Helper function to create a new node */
Node* newnode(int data)
{
    Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
void printMatrix(){
        for(int i=0;i<size;i++){
            for(int j=0;j<size;j++)
                cout<<M[i][j]<<" ";
            cout<<endl;
        }       
 
}
//First PreOrder Traversal
void MatrixUtil(Node *root,int index){
         
    if(root==NULL)return;
     
        int preData=root->data;
             
        //Since there is no ancestor for root node,
        // so we doesn't assign it's value as 1           
        if(index==-1)index=root->data;
        else  M[index][preData]=1;   
     
    MatrixUtil(root->left,preData);
    MatrixUtil(root->right,preData);
}
 
void Matrix(Node *root){
    // Call Func MatrixUtil
    MatrixUtil(root,-1);
     
     
    //Applying Transitive Closure for the given Matrix
    for(int i=0;i<size;i++){
        for (int j = 0;j< size; j++) {
            for(int k=0;k<size;k++)
                M[j][k]=M[j][k]||(M[j][i]&&M[i][k]);
             
        }
         
    }
     
    //Printing Matrix
    printMatrix();
 
     
}
/* Driver program to test above functions*/
int main()
{
    Node *root     = newnode(5);
    root->left     = newnode(1);
    root->right     = newnode(2);
    root->left->left = newnode(0);
    root->left->right = newnode(4);
    root->right->left = newnode(3);
 
    Matrix(root);
     
 
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
/* C++ program to construct tree
from inorder traversal */
#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;
};
 
/* Prototypes of a utility function to get the maximum
value in inorder[start..end] */
int max(int inorder[], int strt, int end);
 
/* A utility function to allocate memory for a node */
node* newNode(int data);
 
/* Recursive function to construct binary of size len from
Inorder traversal inorder[]. Initial values of start and end
should be 0 and len -1. */
node* buildTree (int inorder[], int start, int end)
{
    if (start > end)
        return NULL;
 
    /* Find index of the maximum element from Binary Tree */
    int i = max (inorder, start, end);
 
    /* Pick the maximum value and make it root */
    node *root = newNode(inorder[i]);
 
    /* If this is the only element in inorder[start..end],
    then return it */
    if (start == end)
        return root;
 
    /* Using index in Inorder traversal, construct left and
    right subtress */
    root->left = buildTree (inorder, start, i - 1);
    root->right = buildTree (inorder, i + 1, end);
 
    return root;
}
 
/* UTILITY FUNCTIONS */
/* Function to find index of the maximum value in arr[start...end] */
int max (int arr[], int strt, int end)
{
    int i, max = arr[strt], maxind = strt;
    for(i = strt + 1; i <= end; i++)
    {
        if(arr[i] > max)
        {
            max = arr[i];
            maxind = i;
        }
    }
    return maxind;
}
 
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
node* newNode (int 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()
{
    /* Assume that inorder traversal of following tree is given
        40
        / \
        10 30
        /     \
        5     28 */
 
    int inorder[] = {5, 10, 40, 30, 28};
    int len = sizeof(inorder)/sizeof(inorder[0]);
    node *root = buildTree(inorder, 0, len - 1);
 
    /* Let us test the built tree by printing Inorder traversal */
    cout << "Inorder traversal of the constructed tree is \n";
    printInorder(root);
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: linear
// C++ program to construct a Binary Tree from parent array
#include <bits/stdc++.h>
using namespace std;
 
 
struct Node {
    int data;
    struct Node* left = NULL;
    struct Node* right = NULL;
    Node() {}
 
    Node(int x) { data = x; }
};
 
// Function to construct binary tree from parent array.
Node* createTree(int parent[], int n)
{
    // Create an array to store the reference
    // of all newly created nodes corresponding
    // to node value
    vector<Node*> ref;
 
    // This root represent the root of the
    // newly constructed tree
    Node* root = new Node();
 
    // Create n new tree nodes, each having
    // a value from 0 to n-1, and store them
    // in ref
    for (int i = 0; i < n; i++) {
        Node* temp = new Node(i);
        ref.push_back(temp);
    }
 
    // Traverse the parent array and build the tree
    for (int i = 0; i < n; i++) {
 
        // If the parent is -1, set the root
        // to the current node having
        // the value i which is stored in ref[i]
        if (parent[i] == -1) {
            root = ref[i];
        }
        else {
            // Check if the parent's left child
            // is NULL then map the left child
            // to its parent.
            if (ref[parent[i]]->left == NULL)
                ref[parent[i]]->left = ref[i];
            else
                ref[parent[i]]->right = ref[i];
        }
    }
 
    // Return the root of the newly constructed tree
    return root;
}
 
// Function for inorder traversal
void inorder(Node* root)
{
    if (root != NULL) {
        inorder(root->left);
        cout << root->data << " ";
        inorder(root->right);
    }
}
 
// Driver code
int main()
{
    int parent[] = { -1, 0, 0, 1, 1, 3, 5 };
    int n = sizeof parent / sizeof parent[0];
    Node* root = createTree(parent, n);
    cout << "Inorder Traversal of constructed tree\n";
    inorder(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
/* C++ program to construct tree using inorder and
   postorder 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 {
    int data;
    Node *left, *right;
};
 
// Utility function to create a new node
Node* newNode(int data);
 
/* Prototypes for utility functions */
int search(int arr[], int strt, int end, int value);
 
/* Recursive function to construct binary of size n
   from  Inorder traversal in[] and Postorder traversal
   post[].  Initial values of inStrt and inEnd should
   be 0 and n -1.  The function doesn't do any error
   checking for cases where inorder and postorder
   do not form a tree */
Node* buildUtil(int in[], int post[], int inStrt,
                int inEnd, int* pIndex)
{
    // Base case
    if (inStrt > inEnd)
        return NULL;
 
    /* Pick current node from Postorder traversal using
       postIndex and decrement postIndex */
    Node* node = newNode(post[*pIndex]);
    (*pIndex)--;
 
    /* If this node has no children then return */
    if (inStrt == inEnd)
        return node;
 
    /* Else find the index of this node in Inorder
       traversal */
    int iIndex = search(in, inStrt, inEnd, node->data);
 
    /* Using index in Inorder traversal, construct left and
       right subtrees */
    node->right = buildUtil(in, post, iIndex + 1, inEnd, pIndex);
    node->left = buildUtil(in, post, inStrt, iIndex - 1, pIndex);
 
    return node;
}
 
// This function mainly initializes index of root
// and calls buildUtil()
Node* buildTree(int in[], int post[], int n)
{
    int pIndex = n - 1;
    return buildUtil(in, post, 0, n - 1, &pIndex);
}
 
/* Function to find index of value in arr[start...end]
   The function assumes that value is postsent in in[] */
int search(int arr[], int strt, int end, int value)
{
    int i;
    for (i = strt; i <= end; i++) {
        if (arr[i] == value)
            break;
    }
    return i;
}
 
/* 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);
}
 
// Driver code
int main()
{
    int in[] = { 4, 8, 2, 5, 1, 6, 3, 7 };
    int post[] = { 8, 4, 5, 2, 6, 7, 3, 1 };
    int n = sizeof(in) / sizeof(in[0]);
 
    Node* root = buildTree(in, post, n);
 
    cout << "Preorder of the constructed tree : \n";
    preOrder(root);
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
/* C++ program to construct tree using inorder and
postorder 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 {
    int data;
    Node *left, *right;
};
 
// Utility function to create a new node
Node* newNode(int data);
 
/* Recursive function to construct binary of size n
from Inorder traversal in[] and Postorder traversal
post[]. Initial values of inStrt and inEnd should
be 0 and n -1. The function doesn't do any error
checking for cases where inorder and postorder
do not form a tree */
Node* buildUtil(int in[], int post[], int inStrt,
    int inEnd, int* pIndex, unordered_map<int, int>& mp)
{
    // Base case
    if (inStrt > inEnd)
        return NULL;
 
    /* Pick current node from Postorder traversal 
    using postIndex and decrement postIndex */
    int curr = post[*pIndex];
    Node* node = newNode(curr);
    (*pIndex)--;
 
    /* If this node has no children then return */
    if (inStrt == inEnd)
        return node;
 
    /* Else find the index of this node in Inorder
    traversal */
    int iIndex = mp[curr];
 
    /* Using index in Inorder traversal, construct
    left and right subtrees */
    node->right = buildUtil(in, post, iIndex + 1,
                            inEnd, pIndex, mp);
    node->left = buildUtil(in, post, inStrt,
                           iIndex - 1, pIndex, mp);
 
    return node;
}
 
// This function mainly creates an unordered_map, then
// calls buildTreeUtil()
struct Node* buildTree(int in[], int post[], int len)
{
    // Store indexes of all items so that we
    // we can quickly find later
    unordered_map<int, int> mp;
    for (int i = 0; i < len; i++)
        mp[in[i]] = i;
 
    int index = len - 1; // Index in postorder
    return buildUtil(in, post, 0, len - 1,
                     &index, mp);
}
 
/* 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);
}
 
// Driver code
int main()
{
    int in[] = { 4, 8, 2, 5, 1, 6, 3, 7 };
    int post[] = { 8, 4, 5, 2, 6, 7, 3, 1 };
    int n = sizeof(in) / sizeof(in[0]);
 
    Node* root = buildTree(in, post, n);
 
    cout << "Preorder of the constructed tree : \n";
    preOrder(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program for above approach
#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;
  Node(int x)
  {
    data = x;
    left = right = NULL;
  }
};
 
/*Tree building function*/
Node *buildTree(int in[], int post[], int n)
{
 
  // Create Stack of type Node*
  stack<Node*> st;
 
  // Create Set of type Node*
  set<Node*> s;
 
  // Initialise postIndex with n - 1
  int postIndex = n - 1;
 
  // Initialise root with NULL
  Node* root = NULL;
 
  for (int p = n - 1, i = n - 1; p>=0;) 
  {
 
    // Initialise node with NULL
    Node* node = NULL;
     
    // Run do-while loop
    do
    {
 
      // Initialise node with
      // new Node(post[p]);
      node = new Node(post[p]);
 
      // Check is root is
      // equal to NULL
      if (root == NULL)
      {
        root = node;
      }
       
      // If size of set
      // is greater than 0
      if (st.size() > 0) 
      {
         
        // If st.top() is present in the
        // set s
        if (s.find(st.top()) != s.end())
        {
          s.erase(st.top());
          st.top()->left = node;
          st.pop();
        }
        else
        {
          st.top()->right = node;
        }
      }
       
      st.push(node);
       
    } while (post[p--] != in[i] && p >=0);
 
    node = NULL;
     
    // If the stack is not empty and
    // st.top()->data is equal to in[i]
    while (st.size() > 0 && i>=0 && 
           st.top()->data == in[i]) 
    {
      node = st.top();
       
      // Pop elements from stack
      st.pop();
      i--;
    }
     
    // if node not equal to NULL
    if (node != NULL) 
    {
      s.insert(node);
      st.push(node);
    }
  }
   
  // Return root
  return root;
 
}
/* for print preOrder Traversal */
void preOrder(Node* node)
{
  if (node == NULL)
    return;
  printf("%d ", node->data);
  preOrder(node->left);
  preOrder(node->right);
}
 
// Driver Code
int main()
{
 
  int in[] = { 4, 8, 2, 5, 1, 6, 3, 7 };
  int post[] = { 8, 4, 5, 2, 6, 7, 3, 1 };
  int n = sizeof(in) / sizeof(in[0]);
 
  // Function Call
  Node* root = buildTree(in, post, n);
 
  cout << "Preorder of the constructed tree : \n";
 
  // Function Call for preOrder
  preOrder(root);
  return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to create a doubly linked list out
// of given a ternary tree.
#include <bits/stdc++.h>
using namespace std;
 
/* A ternary tree */
struct Node
{
    int data;
    struct Node *left, *middle, *right;
};
 
/* Helper function that allocates a new node with the
   given data and assign NULL to left, middle and right
   pointers.*/
Node* newNode(int data)
{
    Node* node = new Node;
    node->data = data;
    node->left = node->middle = node->right = NULL;
    return node;
}
 
/* Utility function that constructs doubly linked list
by inserting current node at the end of the doubly
linked list by using a tail pointer */
void push(Node** tail_ref, Node* node)
{
    // initialize tail pointer
    if (*tail_ref == NULL)
    {
        *tail_ref = node;
 
        // set left, middle and right child to point
        // to NULL
        node->left = node->middle = node->right = NULL;
 
        return;
    }
 
    // insert node in the end using tail pointer
    (*tail_ref)->right = node;
 
    // set prev of node
    node->left = (*tail_ref);
 
    // set middle and right child to point to NULL
    node->right = node->middle = NULL;
 
    // now tail pointer will point to inserted node
    (*tail_ref) = node;
}
 
/* Create a doubly linked list out of given a ternary tree.
by traversing the tree in preorder fashion. */
void TernaryTreeToList(Node* root, Node** head_ref)
{
    // Base case
    if (root == NULL)
        return;
 
    //create a static tail pointer
    static Node* tail = NULL;
 
    // store left, middle and right nodes
    // for future calls.
    Node* left = root->left;
    Node* middle = root->middle;
    Node* right = root->right;
 
    // set head of the doubly linked list
    // head will be root of the ternary tree
    if (*head_ref == NULL)
        *head_ref = root;
 
    // push current node in the end of DLL
    push(&tail, root);
 
    //recurse for left, middle and right child
    TernaryTreeToList(left, head_ref);
    TernaryTreeToList(middle, head_ref);
    TernaryTreeToList(right, head_ref);
}
 
// Utility function for printing double linked list.
void printList(Node* head)
{
    printf("Created Double Linked list is:\n");
    while (head)
    {
        printf("%d ", head->data);
        head = head->right;
    }
}
 
// Driver program to test above functions
int main()
{
    // Constructing ternary tree as shown in above figure
    Node* root = newNode(30);
 
    root->left = newNode(5);
    root->middle = newNode(11);
    root->right = newNode(63);
 
    root->left->left = newNode(1);
    root->left->middle = newNode(4);
    root->left->right = newNode(8);
 
    root->middle->left = newNode(6);
    root->middle->middle = newNode(7);
    root->middle->right = newNode(15);
 
    root->right->left = newNode(31);
    root->right->middle = newNode(55);
    root->right->right = newNode(65);
 
    Node* head = NULL;
 
    TernaryTreeToList(root, &head);
 
    printList(head);
 
    return 0;
}

Time complexity: linear
Space complexity: linear