def longer_tree(): """Returns a longer tree with a preorder traversal of: [25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90] """ root = TreeNode(25) root.left_child = TreeNode(15) root.left_child.left_child = TreeNode(10) root.left_child.left_child.left_child = TreeNode(4) root.left_child.left_child.right_child = TreeNode(12) root.left_child.right_child = TreeNode(22) root.left_child.right_child.left_child = TreeNode(18) root.left_child.right_child.right_child = TreeNode(24) root.right_child = TreeNode(50) root.right_child.left_child = TreeNode(35) root.right_child.left_child.left_child = TreeNode(31) root.right_child.left_child.right_child = TreeNode(44) root.right_child.right_child = TreeNode(70) root.right_child.right_child.left_child = TreeNode(66) root.right_child.right_child.right_child = TreeNode(90) yield root del root
def _zig(self, node: TreeNode): parent = node.parent is_right_zig = parent.right_child is node node_right = node.right_child node_left = node.left_child if parent is self.root: self.root = node node.parent = None else: node.parent = parent.parent if is_right_zig: node.left_child = parent node.right_child = node_right parent.right_child = node_left else: node.left_child = node_left node.right_child = parent parent.left_child = node_right
def basic_tree(): """Returns a basic tree with a preorder traversal of: [1, 2, 4, 5, 3] an inorder traversal of: [4, 2, 5, 1, 3] and a postorder traversal of: [4, 5, 2, 3, 1] """ root = TreeNode(1) root.left_child = TreeNode(2) root.left_child.left_child = TreeNode(4) root.left_child.right_child = TreeNode(5) root.right_child = TreeNode(3) yield root del root