def mirrortree(root): if root == None: return mroot = tree.Node(root.value) mroot.left_child = mirrortree(root.right_child) mroot.right_child = mirrortree(root.left_child) return mroot
def mergetree(root1, root2): if root1 == None and root2 == None: return if root1 and root2 == None: out = tree.Node(root1.value) out.left_child = mergetree(root1.left_child, None) out.right_child = mergetree(root1.right_child, None) if root2 and root1 == None: out = tree.Node(root2.value) out.left_child = mergetree(None, root2.left_child) out.right_child = mergetree(None, root2.right_child) if root1 and root2: out = tree.Node(root1.value + root2.value) out.left_child = mergetree(root1.left_child, root2.left_child) out.right_child = mergetree(root1.right_child, root2.right_child) return out
def arrayToBst(a): n = len(a) if n >= 1: node = Tree.Node(a[n // 2]) node.left_child = arrayToBst(a[(n // 2) + 1:]) node.right_child = arrayToBst(a[:(n // 2)]) return node
def Tree_Pre_Inorder(a, b): if len(a) > 0: root = tree.Node(a[0]) index = b.index(a[0]) lefta = a[1:index + 1] leftb = b[:index] righta = a[index + 1:] rightb = b[index + 1:] root.left_child = Tree_Pre_Inorder(lefta, leftb) root.right_child = Tree_Pre_Inorder(righta, rightb) return root