Banana-Leopard/TASTY huggingface dataset:
# Python3 program to construct tree using inorder
# and postorder traversals
 
# A binary tree node has data, pointer to left
# child and a pointer to right child
class Node:
     
    def __init__(self, x):
         
        self.data = x
        self.left = None
        self.right = None
 
# 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
def buildUtil(inn, post, innStrt, innEnd):
     
    global mp, index
     
    # Base case
    if (innStrt > innEnd):
        return None
 
    # Pick current node from Postorder traversal
    # using postIndex and decrement postIndex
    curr = post[index]
    node = Node(curr)
    index -= 1
 
    # If this node has no children then return
    if (innStrt == innEnd):
        return node
 
    # Else find the index of this node inn
    # Inorder traversal
    iIndex = mp[curr]
 
    # Using index inn Inorder traversal,
    # construct left and right subtrees
    node.right = buildUtil(inn, post,
                           iIndex + 1, innEnd)
    node.left = buildUtil(inn, post, innStrt,
                          iIndex - 1)
 
    return node
 
# This function mainly creates an unordered_map,
# then calls buildTreeUtil()
def buildTree(inn, post, lenn):
     
    global index
     
    # Store indexes of all items so that we
    # we can quickly find later
    for i in range(lenn):
        mp[inn[i]] = i
         
    # Index in postorder
    index = lenn - 1
    return buildUtil(inn, post, 0, lenn - 1)
 
# This function is here just to test
def preOrder(node):
     
    if (node == None):
        return
         
    print(node.data, end = " ")
    preOrder(node.left)
    preOrder(node.right)
 
# Driver Code
if __name__ == '__main__':
     
    inn = [ 4, 8, 2, 5, 1, 6, 3, 7 ]
    post = [ 8, 4, 5, 2, 6, 7, 3, 1 ]
    n = len(inn)
    mp, index = {}, 0
 
    root = buildTree(inn, post, n)
 
    print("Preorder of the constructed tree :")
    preOrder(root)
 
# This code is contributed by mohit kumar 29

Time complexity: linear
Space complexity: linear
# Python program for above approach
 
# A binary tree node has data, pointer
# to left   child and a pointer to right
# child
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None
 
# Tree building function
def buildTree(inorder, post, n):
    # Create Stack of type Node
    st = []
    # Create Set of type Node
    set = []
    # Initialise postIndex with n - 1
    postIndex = n - 1
    # Initialise root with NULL
    root = None
 
    p = n-1
    i = n-1
 
    while p >= 0:
        # Initialise node with NULL
        node = None
 
        #  Run loop
        while True:
 
            # initialize new node
            node = Node(post[p])
 
            # check if root is equal to null
            if root == None:
                root = node
 
            # If size of set is greater than 0
            if len(st) > 0:
 
                # If st top is present in the set s
                if st[-1] in set:
                    set.remove(st[-1])
                    st[-1].left = node
                    st.pop()
                else:
                    st[-1].right = node
 
            st.append(node)
 
            p -= 1
            if post[p+1] == inorder[i] or p < 0:
                break
 
        node = None
 
        # If the stack is not empty and st top data is equal to in[i]
        while len(st) > 0 and i >= 0 and st[-1].data == inorder[i]:
            node = st[-1]
            # Pop elements from stack
            st.pop()
            i -= 1
 
        # if node not equal to None
        if node != None:
            set.append(node)
            st.append(node)
 
    # Return root
    return root
 
#  for print preOrder Traversal
 
 
def preOrder(node):
    if node == None:
        return
    print(node.data, end=" ")
    preOrder(node.left)
    preOrder(node.right)
 
 
# Driver Code
if __name__ == '__main__':
    inorder = [4, 8, 2, 5, 1, 6, 3, 7]
    post = [8, 4, 5, 2, 6, 7, 3, 1]
    n = len(inorder)
 
    # Function Call
    root = buildTree(inorder, post, n)
 
    print("Preorder of the constructed tree :")
 
    # Function Call for preOrder
    preOrder(root)
 
# This code is contributed by Tapesh(tapeshdua420)

Time complexity: linear
Space complexity: linear
# Python3 program to create a doubly linked
# list out of given a ternary tree.
   
# Custom node class.
class newNode:
     
    def __init__(self, data):
         
        self.data = data
        self.left = None
        self.right = None
        self.middle = None
 
class GFG:
     
    def __init__(self):
         
        # Tail of the linked list.
        self.tail = None
 
    # Function to push the node to the tail.
    def push(self, node):
 
        # To put the node at the end of
        # the already existing tail.
        self.tail.right = node
 
        # To point to the previous node.
        node.left = self.tail
 
        # Middle pointer should point to 
        # nothing so null. initiate right
        # pointer to null.
        node.middle = node.right = None
 
        # Update the tail position.
        self.tail = node
 
    # Create a doubly linked list out of given
    # a ternary tree By traversing the tree in
    # preorder fashion.
    def ternaryTree(self, node, head):
         
        if node == None:
            return
 
        left = node.left
        middle = node.middle
        right = node.right
         
        if self.tail != node:
             
            # Already root is in the tail so dont push 
            # the node when it was root.In the first 
            # case both node and tail have root in them.
            self.push(node)
 
        # First the left child is to be taken.
        # Then middle and then right child.
        self.ternaryTree(left, head) 
        self.ternaryTree(middle, head)
        self.ternaryTree(right, head)
 
    def startTree(self, root):
         
        # Initiate the head and tail with root.
        head = root
        self.tail = root
        self.ternaryTree(root, head)
 
        # Since the head,root are passed 
        # with reference the changes in 
        # root will be reflected in head.
        return head
 
    # Utility function for printing double linked list.
    def printList(self, head):
         
        print("Created Double Linked list is:")
         
        while head:
            print(head.data, end = " ")
            head = head.right
 
# Driver code
if __name__ == '__main__':
     
    # Constructing ternary tree as shown
    # in above figure
    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)
 
    # The function which initiates the list 
    # process returns the head.
    head = None
    gfg = GFG()
    head = gfg.startTree(root)
     
    gfg.printList(head)
 
# This code is contributed by Winston Sebastian Pais

Time complexity: linear
Space complexity: linear
# Python3 program to conStruct a
# binary tree from the given String
 
# Helper class that allocates a new node
 
 
class newNode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = None
 
# This function is here just to test
 
 
def preOrder(node):
    if (node == None):
        return
    print(node.data, end=' ')
    preOrder(node.left)
    preOrder(node.right)
 
# function to return the index of
# close parenthesis
 
 
def findIndex(Str, si, ei):
    if (si > ei):
        return -1
 
    # Inbuilt stack
    s = []
    for i in range(si, ei + 1):
 
        # if open parenthesis, push it
        if (Str[i] == '('):
            s.append(Str[i])
 
        # if close parenthesis
        elif (Str[i] == ')'):
            if (s[-1] == '('):
                s.pop(-1)
 
                # if stack is empty, this is
                # the required index
                if len(s) == 0:
                    return i
    # if not found return -1
    return -1
 
# function to conStruct tree from String
 
 
def treeFromString(Str, si, ei):
 
    # Base case
    if (si > ei):
        return None
 
    # new root
    root = newNode(ord(Str[si]) - ord('0'))
    index = -1
 
    # if next char is '(' find the
    # index of its complement ')'
    if (si + 1 <= ei and Str[si + 1] == '('):
        index = findIndex(Str, si + 1, ei)
 
    # if index found
    if (index != -1):
 
        # call for left subtree
        root.left = treeFromString(Str, si + 2,
                                   index - 1)
 
        # call for right subtree
        root.right = treeFromString(Str, index + 2,
                                    ei - 1)
    return root
 
 
# Driver Code
if __name__ == '__main__':
    Str = "4(2(3)(1))(6(5))"
    root = treeFromString(Str, 0, len(Str) - 1)
    preOrder(root)
 
# This code is contributed by pranchalK

Time complexity: quadratic
Space complexity: linear
# A simple inorder traversal based program to convert a
# Binary Tree to DLL
 
# A Binary Tree node
class Node:
     
    # Constructor to create a new tree node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# Standard Inorder traversal of tree
def inorder(root):
     
    if root is not None:
        inorder(root.left)
        print ("\t%d" %(root.data),end=" ")
        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
def fixPrevPtr(root):
    if root is not None:
        fixPrevPtr(root.left)
        root.left = fixPrevPtr.pre
        fixPrevPtr.pre = root
        fixPrevPtr(root.right)
 
# Changes right pointers to work as next pointers in
# converted DLL
def fixNextPtr(root):
 
    prev = None
    # Find the right most node in BT or last node in DLL
    while(root and root.right != None):
        root = root.right
 
    # Start from the rightmost node, traverse back using
    # left pointers
    # While traversing, change right pointer of nodes
    while(root and root.left != None):
        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
def BTToDLL(root):
     
    # Set the previous pointer
    fixPrevPtr(root)
 
    # Set the next pointer and return head of DLL
    return fixNextPtr(root)
 
# Traversses the DLL from left to right
def printList(root):
    while(root != None):
        print ("\t%d" %(root.data),end=" ")
        root = root.right
 
# Driver program to test above function
root = Node(10)
root.left = Node(12)
root.right = Node(15)
root.left.left = Node(25)
root.left.right = Node(30)
root.right.left = Node(36)
 
print ("Inorder Tree Traversal")
inorder(root)
 
# Static variable pre for function fixPrevPtr
fixPrevPtr.pre = None
head = BTToDLL(root)
 
print ("\nDLL Traversal")
printList(head)
     
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

Time complexity: linear
Space complexity: linear
# Python3 program to convert a given Binary Tree to Doubly Linked List 
class Node:
    def __init__(self, data):
        self.data = data
        self.left = self.right = None
  
class BinaryTree:
    # 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 
    root, head = None, None
      
    def BToDll(self, root: Node):
        if root is None:
            return
  
        # Recursively convert right subtree
        self.BToDll(root.right)
  
        # Insert root into doubly linked list
        root.right = self.head
  
        # Change left pointer of previous head
        if self.head is not None:
            self.head.left = root
  
        # Change head of doubly linked list
        self.head = root
  
        # Recursively convert left subtree
        self.BToDll(root.left)
  
    @staticmethod
    def print_list(head: Node):
        print('Extracted Double Linked list is:')
        while head is not None:
            print(head.data, end = ' ')
            head = head.right
  
# Driver program to test above function 
if __name__ == '__main__':
      
    """
    Constructing below tree
            5
        // \\
        3 6
        // \\ \\
        1 4 8
    // \\ // \\
    0 2 7 9
    """
    tree = BinaryTree()
    tree.root = Node(5)
    tree.root.left = Node(3)
    tree.root.right = Node(6)
    tree.root.left.left = Node(1)
    tree.root.left.right = Node(4)
    tree.root.right.right = Node(8)
    tree.root.left.left.left = Node(0)
    tree.root.left.left.right = Node(2)
    tree.root.right.right.left = Node(7)
    tree.root.right.right.right = Node(9)
  
    tree.BToDll(tree.root)
    tree.print_list(tree.head)
  
# This code is contributed by Rajat Srivastava

Time complexity: linear
Space complexity: linear
# Python3 program to convert a tree
# into its sum tree
 
# Node definition
class node:
     
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data
 
# Convert a given tree to a tree where
# every node contains sum of values of
# nodes in left and right subtrees
# in the original tree
def toSumTree(Node) :
     
    # Base case
    if(Node == None) :
        return 0
 
    # Store the old value
    old_val = Node.data
 
    # Recursively call for left and
    # right subtrees and store the sum as
    # new value of this node
    Node.data = toSumTree(Node.left) + \
                toSumTree(Node.right)
 
    # Return the sum of values of nodes
    # in left and right subtrees and
    # old_value of this node
    return Node.data + old_val
 
# A utility function to print
# inorder traversal of a Binary Tree
def printInorder(Node) :
    if (Node == None) :
        return
    printInorder(Node.left)
    print(Node.data, end = " ")
    printInorder(Node.right)
     
# Utility function to create a new Binary Tree node
def newNode(data) :
    temp = node(0)
    temp.data = data
    temp.left = None
    temp.right = None
     
    return temp
 
# Driver Code
if __name__ == "__main__":
    root = None
    x = 0
     
    # Constructing tree given in the above figure
    root = newNode(10)
    root.left = newNode(-2)
    root.right = newNode(6)
    root.left.left = newNode(8)
    root.left.right = newNode(-4)
    root.right.left = newNode(7)
    root.right.right = newNode(5)
     
    toSumTree(root)
     
    # Print inorder traversal of the converted
    # tree to test result of toSumTree()
    print("Inorder Traversal of the resultant tree is: ")
    printInorder(root)
 
# This code is contributed by Arnab Kundu

Time complexity: constant
Space complexity: constant
# Python3 program to store sum of nodes
# in left subtree in every node
 
# Binary Tree Node
 
# utility that allocates a new Node
# with the given key
class newNode:
 
    # Construct to create a new node
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
 
# Function to modify a Binary Tree so
# that every node stores sum of values
# in its left child including its own value
def updatetree(root):
     
    # Base cases
    if (not root):
        return 0
    if (root.left == None and
        root.right == None) :
        return root.data
 
    # Update left and right subtrees
    leftsum = updatetree(root.left)
    rightsum = updatetree(root.right)
 
    # Add leftsum to current node
    root.data += leftsum
 
    # Return sum of values under root
    return root.data + rightsum
 
# Utility function to do inorder traversal
def inorder(node) :
 
    if (node == None) :
        return
    inorder(node.left)
    print(node.data, end = " ")
    inorder(node.right)
 
# Driver Code
if __name__ == '__main__':
     
    """ Let us construct below tree
                    1
                / \
                2 3
                / \ \
                4 5 6 """
    root = newNode(1)
    root.left = newNode(2)
    root.right = newNode(3)
    root.left.left = newNode(4)
    root.left.right = newNode(5)
    root.right.right = newNode(6)
 
    updatetree(root)
 
    print("Inorder traversal of the modified tree is")
    inorder(root)
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

Time complexity: linear
Space complexity: linear
# Python3 program to convert a binary
# tree to its mirror
 
# Utility function to create a new
# tree node
 
 
class newNode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = None
 
 
""" 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
"""
 
 
def mirror(node):
 
    if (node == None):
        return
    else:
 
        temp = node
 
        """ 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."""
 
 
def inOrder(node):
 
    if (node == None):
        return
 
    inOrder(node.left)
    print(node.data, end=" ")
    inOrder(node.right)
 
 
# Driver code
if __name__ == "__main__":
 
    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 """
    print("Inorder traversal of the",
          "constructed tree is")
    inOrder(root)
 
    """ Convert tree to its mirror """
    mirror(root)
 
    """ Print inorder traversal of
        the mirror tree """
    print("\nInorder traversal of",
          "the mirror tree is ")
    inOrder(root)
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

Time complexity: linear
Space complexity: linear
# Python3 program to convert a Binary
# Tree to its mirror
 
# A binary tree node has data, pointer to
# left child and a pointer to right child
# Helper function that allocates a new node
# with the given data and None left and
# right pointers
 
 
class newNode:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
 
''' 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
    '''
 
 
def mirror(root):
 
    if (root == None):
        return
 
    q = []
    q.append(root)
 
    # Do BFS. While doing BFS, keep swapping
    # left and right children
    while (len(q)):
 
        # pop top node from queue
        curr = q[0]
        q.pop(0)
 
        # swap left child with right child
        curr.left, curr.right = curr.right, curr.left
 
        # append left and right children
        if (curr.left):
            q.append(curr.left)
        if (curr.right):
            q.append(curr.right)
 
 
""" Helper function to print Inorder traversal."""
 
 
def inOrder(node):
    if (node == None):
        return
    inOrder(node.left)
    print(node.data, end=" ")
    inOrder(node.right)
 
 
# Driver code
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 """
print("Inorder traversal of the constructed tree is")
inOrder(root)
 
""" Convert tree to its mirror """
mirror(root)
 
""" Print inorder traversal of the mirror tree """
print("\nInorder traversal of the mirror tree is")
inOrder(root)
 
# This code is contributed by SHUBHAMSINGH10

Time complexity: linear
Space complexity: linear
// C Program to convert a Binary Tree
// to a Circular Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
 
// To represents a node of a Binary Tree
typedef struct Node {
    struct Node *left, *right;
    int data;
} Node;
 
// 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)
{
    printf("Circular Linked List is :\n");
    Node* itr = head;
    do {
        printf("%d ", itr->data);
        itr = itr->right;
    } while (head != itr);
    printf("\n");
}
 
// Create a new Node and return its address
Node* newNode(int data)
{
    Node* temp = (Node*)malloc(sizeof(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