def construct(length: int) -> Optional[Node]: nonlocal head if head is None or length == 0: return None left = construct(length // 2) root = Node(head.data) head = head.next root.left = left root.right = construct(length - length // 2 - 1) return root
def tree() -> Optional[Node]: nonlocal index if index >= l: return None node = Node(preorder[index]) index += 1 if pre_ln[index - 1] == "N": node.left = tree() node.right = tree() return node
def merge_sum_tree(augend: Optional[Node], addend: Optional[Node]) -> Optional[Node]: if augend is None and addend is None: return None if augend is None: return addend if addend is None: return augend root = Node(augend.data + addend.data) root.left = merge_sum_tree(augend.left, addend.left) root.right = merge_sum_tree(augend.right, addend.right) return root
def tree(start: int, end: int) -> Optional[Node]: nonlocal pre_index if pre_index >= l or start > end: return None value = preorder[pre_index] node = Node(value) in_index = d.get(value) or -1 pre_index += 1 if start < end: node.left = tree(start, in_index - 1) node.right = tree(in_index + 1, end) return node
def construct(): nonlocal index, length if index >= length: return None root = Node(expression[index]) index += 1 if index < length - 1: if expression[index] == "?": index += 1 root.left = construct() index += 1 root.right = construct() return root
def tree(start: int, end: int) -> Optional[Node]: nonlocal pre_index if start > end or pre_index >= l: return None node = Node(preorder[pre_index]) pre_index += 1 if start == end or pre_index >= l: return node mirror_index = d[preorder[pre_index]] if mirror_index <= end: node.left = tree(mirror_index, end) node.right = tree(start + 1, mirror_index - 1) return node
def tree(start: int, end: int) -> Optional[Node]: nonlocal pre_index if pre_index >= l or start > end: return None value = preorder[pre_index] node = Node(value) pre_index += 1 if start == end or pre_index >= l: return node post_index: int = d[preorder[pre_index]] if post_index <= end: node.left = tree(start, post_index) node.right = tree(post_index + 1, end) return node
if in_iter == 0: node.data = arr[1] elif in_iter == l - 1: node.data = arr[l - 2] else: node.data = arr[in_iter - 1] + arr[in_iter + 1] in_iter += 1 traverse_inorder(node.right, fill_array) arr: list = [] traverse_inorder(root, fill_array=True) in_iter = 0 l = len(arr) traverse_inorder(root, fill_array=False) return root if __name__ == "__main__": root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) print(inorder(root)) root = replace_data(root) print(inorder(root))
def diagonal_traversal(root: Node): queue = deque() queue.append([root, 0]) while queue: node, level = queue.popleft() if node.left: queue.append([node.left, level + 1]) if node.right: queue.appendleft([node.right, level]) if queue and queue[0][1] > level: print(node.data) else: print(node.data, end=" ") if __name__ == "__main__": root = Node(8) root.left = Node(3) root.right = Node(10) root.left.left = Node(1) root.left.right = Node(6) root.right.left = Node(7) root.right.right = Node(14) root.left.right.left = Node(4) root.right.right.left = Node(13) diagonal_traversal(root)
return 0 return_value = root.data if root.left is None and root.right is None: root.data = 0 return return_value left_sum = sum_tree(root.left) right_sum = sum_tree(root.right) root.data = left_sum + right_sum return return_value + root.data if __name__ == "__main__": 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) inorder(root) print() sum_tree(root) inorder(root) print() root = Node(10) root.left = Node(-2) root.right = Node(6) root.left.left = Node(8)
addend: Optional[Node]) -> Optional[Node]: if augend is None and addend is None: return None if augend is None: return addend if addend is None: return augend root = Node(augend.data + addend.data) root.left = merge_sum_tree(augend.left, addend.left) root.right = merge_sum_tree(augend.right, addend.right) return root if __name__ == "__main__": augend = Node(2) augend.left = Node(1) augend.right = Node(4) augend.left.left = Node(5) addend = Node(3) addend.left = Node(6) addend.right = Node(1) addend.left.right = Node(2) addend.right.right = Node(7) root = merge_sum_tree(augend, addend) inorder(root)