def tree(index: int) -> Optional[Node]: if index >= l: return None node = Node(level_order[index]) node.left = tree(2 * index + 1) node.right = tree(2 * index + 2) return node
def construct(min_val: int, max_val: int) -> Optional[Node]: nonlocal index, length if index >= length or preorder[index] < min_val or preorder[ index] > max_val: return None root = Node(preorder[index]) index += 1 root.left = construct(min_val, root.data) root.right = construct(root.data, max_val) return root
def construct_bin_tree(string: str) -> Optional[Node]: length = len(string) if length == 0: return None root = Node(string[0]) if length > 1: left_end_index = get_subtree_string(string, 1, length - 1) root.left = construct_bin_tree(string[2:left_end_index]) if left_end_index < length - 2: root.right = construct_bin_tree(string[left_end_index + 2:length - 1]) return root
def construct_bst(min_value: int = 0, max_value: int = 0) -> Sequence[Optional[Node]]: if min_value > max_value: return [None] root_list: list = [] for i in range(min_value, max_value + 1): left_subtrees = construct_bst(min_value, i - 1) right_subtrees = construct_bst(i + 1, max_value) for left in left_subtrees: for r in right_subtrees: right = r root = Node(i) root.left = left root.right = right root_list.append(root) return root_list
def inorder_dll(tree: Optional[Node]) -> Optional[Node]: if tree: node = Node(tree.data) if tree.left: left = inorder_dll(tree.left) if left: while left.right: left = left.right node.left = left left.right = node if tree.right: right = inorder_dll(tree.right) if right: while right.left: right = right.left node.right = right right.left = node return node return None
def leaves(root: Node): """ Same as inorder traversal """ if root: leaves(root.left) if not (root.left or root.right): print(root.data) leaves(root.right) def boundary_traverse(root: Node): print(root.data) left_boundary(root.left) leaves(root) right_boundary(root.right) if __name__ == "__main__": root_node = Node(1) root_node.left = Node(2) root_node.right = Node(3) root_node.left.left = Node(4) root_node.left.right = Node(5) root_node.right.left = Node(6) root_node.right.right = Node(7) boundary_traverse(root_node)
if right: while right.left: right = right.left node.right = right right.left = node return node return None def print_dll(dll: Node): while dll: print(dll.data) dll = dll.right if __name__ == "__main__": 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) root = inorder_dll(root) while root.left: root = root.left print_dll(root)
if node.left is not None or node.right is not None: left_sum = children_sum(node.left) right_sum = children_sum(node.right) diff = left_sum + right_sum - node.data if diff > 0: node.data += diff elif diff < 0: increment_children_data(node, -diff) return node.data if __name__ == "__main__": """ 50 7 2 3 5 1 30 """ root = Node(50) root.left = Node(7) root.right = Node(2) root.left.left = Node(3) root.left.right = Node(5) root.right.left = Node(1) root.right.right = Node(30) children_sum(root) inorder(root) print()
if root.left is None and root.right is None: return node_level == level if root.left is None or root.right is None: return False return is_full_and_complete(root.left, node_level + 1) and is_full_and_complete( root.right, node_level + 1 ) temp = root level = 0 while temp and temp.left: temp = temp.left level += 1 return is_full_and_complete(root, 0) if __name__ == "__main__": root = Node(10) root.left = Node(20) root.right = Node(30) root.left.left = Node(40) root.left.right = Node(50) root.right.left = Node(60) root.right.right = Node(70) assert is_perfect(root) == True