Banana-Leopard/TASTY huggingface dataset:
# Python3 Program to convert a Binary
# Tree to a Circular Doubly Linked List
 
 
class newNode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = None
 
# A function that appends rightList
# at the end of leftList.
 
 
def concatenate(leftList, rightList):
 
    # If either of the list is empty
    # then return the other list
    if (leftList == None):
        return rightList
    if (rightList == None):
        return leftList
 
    # Store the last Node of left List
    leftLast = leftList.left
 
    # Store the last Node of right List
    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
 
 
def bTreeToCList(root):
    if (root == None):
        return None
 
    # Recursively convert left and
    # right subtrees
    left = bTreeToCList(root.left)
    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
 
 
def displayCList(head):
    print("Circular Linked List is :")
    itr = head
    first = 1
    while (head != itr or first):
        print(itr.data, end=" ")
        itr = itr.right
        first = 0
    print()
 
 
# Driver Code
if __name__ == '__main__':
    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)
 
    head = bTreeToCList(root)
    displayCList(head)
 
# This code is contributed by PranchalK

Time complexity: linear
Space complexity: logn
# Class to define a node 
# structure of the tree
class Node:
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
  
# Function to convert ternary 
# expression to a Binary tree
# It returns the root node 
# of the tree
def convert_expression(expression, i):
    if i >= len(expression):
        return None
  
    # Create a new node object
    # for the expression at
    # ith index
    root = Node(expression[i])
  
    i += 1
  
    # if current character of 
    # ternary expression is '?'
    # then we add next character 
    # as a left child of
    # current node
    if (i < len(expression) and 
                expression[i] is "?"):
        root.left = convert_expression(expression, i + 1)
          
    # else we have to add it 
    # as a right child of
    # current node expression[0] == ':'
    elif i < len(expression):
        root.right = convert_expression(expression, i + 1)
    return root
  
# Function to print the tree
# in a pre-order traversal pattern
def print_tree(root):
    if not root:
        return
    print(root.data, end=' ')
    print_tree(root.left)
    print_tree(root.right)
  
# Driver Code
if __name__ == "__main__":
    string_expression = "a?b?c:d:e"
    root_node = convert_expression(string_expression, 0)
    print_tree(root_node)
  
# This code is contributed
# by Kanav Malhotra

Time complexity: linear
Space complexity: linear
# Python3 program for Minimum swap required
# to convert binary tree to binary search tree
 
# Inorder Traversal of Binary Tree
def inorder(a, n, index):
     
    global v
     
    # If index is greater or equal to
    # vector size
    if (index >= n):
        return
     
    inorder(a, n, 2 * index + 1)
 
    # Push elements in vector
    v.append(a[index])
    inorder(a, n, 2 * index + 2)
 
# Function to find minimum swaps
# to sort an array
def minSwaps():
     
    global v
    t = [[0, 0] for i in range(len(v))]
    ans = -2
 
    for i in range(len(v)):
        t[i][0], t[i][1] = v[i], i
 
    t, i = sorted(t), 0
 
    while i < len(t):
         
        # break
        # second element is equal to i
        if (i == t[i][1]):
            i += 1
            continue
        else:
             
            # Swapping of elements
            t[i][0], t[t[i][1]][0] = t[t[i][1]][0], t[i][0]
            t[i][1], t[t[i][1]][1] = t[t[i][1]][1], t[i][1]
 
        # Second is not equal to i
        if (i == t[i][1]):
            i -= 1
 
        i += 1
 
        ans += 1
 
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    v = []
    a = [ 5, 6, 7, 8, 9, 10, 11 ]
    n = len(a)
    inorder(a, n, 0)
 
    print(minSwaps())
 
# This code is contributed by mohit kumar 29

Time complexity: nlogn
Space complexity: linear
# Python3 program to check children
# sum property
 
# Helper class 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
 
# returns 1 if children sum property
# holds for the given node and both
# of its children
 
 
def isSumProperty(node):
 
    # left_data is left child data and
    # right_data is for right child data
    left_data = 0
    right_data = 0
 
    # If node is None or it's a leaf
    # node then return true
    if(node == None or (node.left == None and
                        node.right == None)):
        return 1
    else:
 
        # If left child is not present then
        # 0 is used as data of left child
        if(node.left != None):
            left_data = node.left.data
 
        # If right child is not present then
        # 0 is used as data of right child
        if(node.right != None):
            right_data = node.right.data
 
        # if the node and both of its children
        # satisfy the property return 1 else 0
        if((node.data == left_data + right_data) and
           isSumProperty(node.left) and
           isSumProperty(node.right)):
            return 1
        else:
            return 0
 
 
# Driver Code
if __name__ == '__main__':
    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)):
        print("The given tree satisfies the",
              "children sum property ")
    else:
        print("The given tree does not satisfy",
              "the children sum property ")
 
# This code is contributed by PranchalK

Time complexity: linear
Space complexity: logn
# Python program to check if two nodes in a binary
# tree are cousins
 
# A Binary Tree Node
class Node:
     
    # Constructor to create a new Binary Tree
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
def isSibling(root, a , b):
 
    # Base Case
    if root is None:
        return 0
 
    return ((root.left == a and root.right ==b) or
            (root.left == b and root.right == a)or
            isSibling(root.left, a, b) or
            isSibling(root.right, a, b))
 
# Recursive function to find level of Node 'ptr' in
# a binary tree
def level(root, ptr, lev):
     
    # Base Case
    if root is None :
        return 0
    if root == ptr:
        return lev
 
    # Return level if Node is present in left subtree
    l = level(root.left, ptr, lev+1)
    if l != 0:
        return l
 
    # Else search in right subtree
    return level(root.right, ptr, lev+1)
 
 
# Returns 1 if a and b are cousins, otherwise 0
def isCousin(root,a, b):
     
    # 1. The two nodes should be on the same level in
    # the binary tree
    # The two nodes should not be siblings(means that
    # they should not have the same parent node
 
    if ((level(root,a,1) == level(root, b, 1)) and
            not (isSibling(root, a, b))):
        return 1
    else:
        return 0
 
 
# Driver program to test above function
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.left.right.right = Node(15)
root.right.left = Node(6)
root.right.right = Node(7)
root.right.left.right = Node(8)
 
node1 = root.left.right
node2 = root.right.right
 
print ("Yes" if isCousin(root, node1, node2) == 1 else "No")
 
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

Time complexity: linear
Space complexity: linear
# Python program to check if all leaves are at same level
 
# A binary tree node
class Node:
     
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# Recursive function which check whether all leaves are at
# same level
def checkUtil(root, level):
     
    # Base Case
    if root is None:
        return True
     
    # If a tree node is encountered
    if root.left is None and root.right is None:
         
        # When a leaf node is found first time
        if check.leafLevel == 0 :
            check.leafLevel = level # Set first leaf found
            return True
 
        # If this is not first leaf node, compare its level
        # with first leaf's level
        return level == check.leafLevel
 
    # If this is not first leaf node, compare its level
    # with first leaf's level
    return (checkUtil(root.left, level+1)and
            checkUtil(root.right, level+1))
 
def check(root):
    level = 0
    check.leafLevel = 0
    return (checkUtil(root, level))
 
# Driver program to test above function
root = Node(12)
root.left = Node(5)
root.left.left = Node(3)
root.left.right = Node(9)
root.left.left.left = Node(1)
root.left.right.left = Node(2)
 
if(check(root)):
    print "Leaves are at same level"
else:
    print "Leaves are not at same level"
 
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

Time complexity: linear
Space complexity: linear
# Python3 program to check if all leaf nodes
# are at same level of binary tree
INT_MAX = 2**31
INT_MIN = -2**31
 
# Tree Node
# returns a new tree Node
class newNode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = None
         
# return true if all leaf nodes are
# at same level, else false
def checkLevelLeafNode(root) :
 
    if (not root) :
        return 1
     
    # create a queue for level
    # order traversal
    q = []
    q.append(root)
     
    result = INT_MAX
    level = 0
 
    # traverse until the queue is empty
    while (len(q)):
        size = len(q)
        level += 1
 
        # traverse for complete level
        while(size > 0 or len(q)):
            temp = q[0]
            q.pop(0)
         
            # check for left child
            if (temp.left) :
                q.append(temp.left)
 
                # if its leaf node
                if(not temp.left.right and
                   not temp.left.left):
 
                    # if it's first leaf node,
                    # then update result
                    if (result == INT_MAX):
                        result = level
                     
                    # if it's not first leaf node,
                    # then compare the level with
                    # level of previous leaf node
                    elif (result != level):
                        return 0                   
                 
            # check for right child
            if (temp.right) :
                q.append(temp.right)
 
                # if it's leaf node
                if (not temp.right.left and
                    not temp.right.right):
 
                    # if it's first leaf node till now,
                    # then update the result
                    if (result == INT_MAX):
                        result = level
                     
                    # if it is not the first leaf node,
                    # then compare the level with level
                    # of previous leaf node
                    elif(result != level):
                        return 0
                size -= 1
    return 1
 
# Driver Code
if __name__ == '__main__':
     
    # construct a tree
    root = newNode(1)
    root.left = newNode(2)
    root.right = newNode(3)
    root.left.right = newNode(4)
    root.right.left = newNode(5)
    root.right.right = newNode(6)
     
    result = checkLevelLeafNode(root)
    if (result) :
        print("All leaf nodes are at same level")
    else:
        print("Leaf nodes not at same level")
 
# This code is contributed by SHUBHAMSINGH10

Time complexity: linear
Space complexity: linear
# Python3 program to check if there
# exist an edge whose removal creates
# two trees of same size
 
# utility function to create a new node
class newNode:
    def __init__(self, x):
        self.data = x
        self.left = self.right = None
 
# To calculate size of tree
# with given root
def count(root):
    if (root == None):
        return 0
    return (count(root.left) +
            count(root.right) + 1)
 
# This function returns true if there
# is an edge whose removal can divide
# the tree in two halves n is size of tree
def checkRec(root, n):
     
    # Base cases
    if (root == None):
        return False
 
    # Check for root
    if (count(root) == n - count(root)):
        return True
 
    # Check for rest of the nodes
    return (checkRec(root.left, n) or
            checkRec(root.right, n))
 
# This function mainly uses checkRec()
def check(root):
     
    # Count total nodes in given tree
    n = count(root)
 
    # Now recursively check all nodes
    return checkRec(root, n)
 
# Driver code
if __name__ == '__main__':
    root = newNode(5)
    root.left = newNode(1)
    root.right = newNode(6)
    root.left.left = newNode(3)
    root.right.left = newNode(7)
    root.right.right = newNode(4)
 
    if check(root):
        print("YES")
    else:
        print("NO")
         
# This code is contributed by PranchalK

Time complexity: quadratic
Space complexity: linear
# Python3 program to check if there exist
# an edge whose removal creates two trees
# of same size
class Node:
     
    def __init__(self, x):
         
        self.key = x
        self.left = None
        self.right = None
 
# To calculate size of tree with
# given root
def count(node):
     
    if (node == None):
        return 0
 
    return (count(node.left) +
            count(node.right) + 1)
 
# This function returns size of tree rooted
# with given root. It also set "res" as true
# if there is an edge whose removal divides
# tree in two halves.n is size of tree
def checkRec(root, n):
     
    global res
     
    # Base case
    if (root == None):
       return 0
 
    # Compute sizes of left and right children
    c = (checkRec(root.left, n) + 1 +
         checkRec(root.right, n))
 
    # If required property is true for
    # current node set "res" as true
    if (c == n - c):
        res = True
 
    # Return size
    return c
 
# This function mainly uses checkRec()
def check(root):
     
    # Count total nodes in given tree
    n = count(root)
 
    # Initialize result and recursively
    # check all nodes
    # bool res = false;
    checkRec(root, n)
 
# Driver code
if __name__ == '__main__':
     
    res = False
    root = Node(5)
    root.left = Node(1)
    root.right = Node(6)
    root.left.left = Node(3)
    root.right.left = Node(7)
    root.right.right = Node(4)
 
    check(root)
     
    if res:
        print("YES")
    else:
        print("NO")
 
# This code is contributed by mohit kumar 29

Time complexity: linear
Space complexity: linear
# Python3 program to check if two Leaf
# Traversal of Two Binary Trees is same or not
 
# Binary Tree node
 
 
class Node:
    def __init__(self, x):
        self.data = x
        self.left = self.right = None
 
    def isLeaf(self):
        return (self.left == None and
                self.right == None)
 
# Returns true of leaf traversal of
# two trees is same, else false
 
 
def isSame(root1, root2):
 
    # Create empty stacks. These stacks are going
    # to be used for iterative traversals.
    s1 = []
    s2 = []
 
    s1.append(root1)
    s2.append(root2)
 
    # Loop until either of two stacks
    # is not empty
    while (len(s1) != 0 or len(s2) != 0):
 
        # If one of the stacks is empty means other
        # stack has extra leaves so return false
        if (len(s1) == 0 or len(s2) == 0):
            return False
 
        temp1 = s1.pop(-1)
        while (temp1 != None and not temp1.isLeaf()):
 
            # append right and left children of temp1.
            # Note that right child is inserted
            # before left
            if (temp1.right != None):
                s1.append(temp1. right)
            if (temp1.left != None):
                s1.append(temp1.left)
                temp1 = s1.pop(-1)
 
        # same for tree2
        temp2 = s2.pop(-1)
        while (temp2 != None and not temp2.isLeaf()):
            if (temp2.right != None):
                s2.append(temp2.right)
            if (temp2.left != None):
                s2.append(temp2.left)
            temp2 = s2.pop(-1)
 
        # If one is None and other is not,
        # then return false
        if (temp1 == None and temp2 != None):
            return False
        if (temp1 != None and temp2 == None):
            return False
 
        # If both are not None and data is
        # not same return false
        if (temp1 != None and temp2 != None):
            if (temp1.data != temp2.data):
                return False
 
    # If control reaches this point,
    # all leaves are matched
    return True
 
 
# Driver Code
if __name__ == '__main__':
 
    # Let us create trees in above example 1
    root1 = Node(1)
    root1.left = Node(2)
    root1.right = Node(3)
    root1.left.left = Node(4)
    root1.right.left = Node(6)
    root1.right.right = Node(7)
 
    root2 = Node(0)
    root2.left = Node(1)
    root2.right = Node(5)
    root2.left.right = Node(4)
    root2.right.left = Node(6)
    root2.right.right = Node(7)
 
    if (isSame(root1, root2)):
        print("Same")
    else:
        print("Not Same")
 
# This code is contributed by pranchalK

Time complexity: linear
Space complexity: nlogn
# Python3 program to find deepest
# left leaf Binary search Tree
 
# Helper function that allocates a 
# new node with the given data and
# None left and right pairs.                                    
class getNode:
 
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# function to check whether a binary 
# tree is a full binary tree or not
def isFullBinaryTree( root) :
 
    # if tree is empty
    if (not root) :
        return True
 
    # queue used for level order
    # traversal
    q = []
 
    # append 'root' to 'q'
    q.append(root)
 
    # traverse all the nodes of the
    # binary tree level by level
    # until queue is empty
    while (not len(q)):
         
        # get the pointer to 'node'
        # at front of queue
        node = q[0]
        q.pop(0)
 
        # if it is a leaf node then continue
        if (node.left == None and
            node.right == None):
            continue
 
        # if either of the child is not None 
        # and the other one is None, then
        # binary tree is not a full binary tee
        if (node.left == None or
            node.right == None):
            return False
 
        # append left and right childs
        # of 'node' on to the queue 'q'
        q.append(node.left)
        q.append(node.right)
     
    # binary tree is a full binary tee
    return True
 
# Driver Code
if __name__ == '__main__':
    root = getNode(1)
    root.left = getNode(2)
    root.right = getNode(3)
    root.left.left = getNode(4)
    root.left.right = getNode(5)
 
    if (isFullBinaryTree(root)) :
        print("Yes" )
    else:
        print("No")
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

Time complexity: linear
Space complexity: constant
""" Program to check if a given Binary
Tree is balanced like a Red-Black Tree """
 
# Helper function that allocates a new
# node with the given data and None
# left and right pointers.                                
class newNode:
 
    # Construct to create a new node
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
 
# Returns returns tree if the Binary
# tree is balanced like a Red-Black
# tree. This function also sets value
# in maxh and minh (passed by
# reference). maxh and minh are set
# as maximum and minimum heights of root.
def isBalancedUtil(root, maxh, minh) :
 
    # Base case
    if (root == None) :
     
        maxh = minh = 0
        return True
     
 
    lmxh=0
     
    # To store max and min
    # heights of left subtree
    lmnh=0
     
    # To store max and min
    # heights of right subtree
    rmxh, rmnh=0,0 
 
    # Check if left subtree is balanced,
    # also set lmxh and lmnh
    if (isBalancedUtil(root.left, lmxh, lmnh) == False) :
        return False
 
    # Check if right subtree is balanced,
    # also set rmxh and rmnh
    if (isBalancedUtil(root.right, rmxh, rmnh) == False) :
        return False
 
    # Set the max and min heights of
    # this node for the parent call
    maxh = max(lmxh, rmxh) + 1
    minh = min(lmnh, rmnh) + 1
 
    # See if this node is balanced
    if (maxh <= 2 * minh) :
        return True
 
    return False
 
 
# A wrapper over isBalancedUtil()
def isBalanced(root) :
 
    maxh, minh =0,0
    return isBalancedUtil(root, maxh, minh)
 
# Driver Code
if __name__ == '__main__':
    root = newNode(10)
    root.left = newNode(5)
    root.right = newNode(100)
    root.right.left = newNode(50)
    root.right.right = newNode(150)
    root.right.left.left = newNode(40)
    if (isBalanced(root)):
        print("Balanced")
    else:
        print("Not Balanced")
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

Time complexity: linear
Space complexity: linear