def convert_util(arr, left, right): if left >= right: return None mid = (left + right) >> 1 root = Node(arr[mid]) root.left = convert_util(arr, left, mid) root.right = convert_util(arr, mid + 1, right) return root
def reconstruct(arr): if not arr: return mid = len(arr) >> 1 root_value = arr[mid] root = Node(root_value) root.left = reconstruct(arr[:mid]) root.right = reconstruct(arr[mid + 1:]) return root
def reconstruct(arr): if not arr: return root = Node(arr[0]) i = 1 while i < len(arr[1:]) and arr[i] < arr[0]: i += 1 left_root = reconstruct(arr[1:i]) right_root = reconstruct(arr[i:]) root.left = left_root root.right = right_root return root
if not root and not path: return True if not root or not path: return False if root.value != path[0]: return False if path[0] == root.value: return check_exists(root.left, path[1:]) or \ check_exists(root.right, path[1:]) if __name__ == "__main__": node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node5 = Node(5) node4.left = node2 node2.left = node1 node2.right = node3 node4.right = node5 print(check_exists(node4, [4, 5])) print(check_exists(node4, [4, 2, 3])) print(check_exists(node4, [4, 2, 1])) print(check_exists(node4, [4, 2, 5])) print(check_exists(node4, [4, 5, 3]))
return False if root1.value != root2.value: return False stack1.append(root1.right) stack1.append(root1.left) stack2.append(root2.right) stack2.append(root2.left) return True if __name__ == "__main__": node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node5 = Node(5) node4.left = node2 node2.left = node1 node2.right = node3 node4.right = node5 node11 = Node(1) node12 = Node(2) node13 = Node(3) node14 = Node(4) node15 = Node(5)
def test(): node1 = Node(1) node3 = Node(3) node4 = Node(4) node6 = Node(6) node7 = Node(7) node8 = Node(8) node10 = Node(10) node13 = Node(13) node14 = Node(14) node8.left = node3 node8.right = node10 node3.left = node1 node3.right = node6 node6.left = node4 node6.right = node7 node10.right = node14 node14.left = node13 expected_result = {0: [8, 10, 14], 1: [3, 6, 7, 13], 2: [1, 4]} actual_result = diagonal_view(node8) assert expected_result == actual_result
lcount, lresult = count_subtrees_util(root.left, left, right) rcount, rresult = count_subtrees_util(root.right, left, right) if lresult and rresult and root.value <= right and root.value >= left: return lcount + rcount + 1, True return lcount + rcount, False def count_subtrees(root, left, right): count, _ = count_subtrees_util(root, left, right) return count if __name__ == "__main__": node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node5 = Node(5) node50 = Node(50) node40 = Node(40) node4.left = node2 node2.left = node1 node2.right = node3 node4.right = node5 node4.right = node40 node40.right = node50 node40.left = node5 print(count_subtrees(node4, 1, 45))
def root_to_leaf(root, parent): root.parent = parent if root.left is None and root.right is None: result = [root.value] while parent: result.append(parent.value) parent = parent.parent print result if root.left is not None: root_to_leaf(root.left, root) if root.right is not None: root_to_leaf(root.right, root) if __name__ == "__main__": node1 = Node(1) node3 = Node(3) node4 = Node(4) node5 = Node(5) node1.left = node3 node1.right = node4 node4.right = node5 root_to_leaf(node1, None)