def setUp(self): self.node = bt.Node(1) self.node.left_child = bt.Node(2) self.node.right_child = None self.node_None = bt.create_binary_tree(None) self.node_None1 = bt.create_binary_tree([None]) self.node_full = bt.create_binary_tree( [1, 2, 3, 4, None, None, 5, None, None, 6, None, None])
rs = deque() q.append(root) while q: for _ in range(len(q)): node = q.popleft() if node is None: continue if node.val == subRoot.val: rs.append(node) q.append(node.left) q.append(node.right) return rs if __name__ == "__main__": trees = [[3, 4, 5, 1, 2], [4, 1, 2], [3, 4, 5, 1, 2, None, None, None, None, 0], [4, 1, 2]] for i in range(0, len(trees), 2): tree = create_binary_tree(trees[i]) subtree = create_binary_tree(trees[i + 1]) printBinaryTree(tree) printBinaryTree(subtree) print(f"rs = {isSubtree(tree, subtree)}")
# return rs, accumulation if root is None: return 0, [] rs_left, acc_left = sum_node(root.left, target) rs_right, acc_right = sum_node(root.right, target) sum_left = [a + root.val for a in acc_left] sum_right = [a + root.val for a in acc_right] acc = [root.val] + sum_left + sum_right cnt = 0 for a in acc: if a == target: cnt += 1 rs = rs_left + rs_right + cnt return rs, acc if __name__ == "__main__": arr = [10, 5, -3, 3, 2, None, 11] arr = [10, 5, -3] target = 0 root = create_binary_tree(arr) cnt = num_path_with_sum(root, target) print(cnt) print(sum_node(root, target)[0])
rs1 = find_node(root.left, p1, p2) rs2 = find_node(root.right, p1, p2) rs.r1 = rs.r1 or rs1.r1 or rs2.r1 rs.r2 = rs.r2 or rs1.r2 or rs2.r2 if rs1.node is not None: rs.node = rs1.node if rs2.node is not None: rs.node = rs2.node if rs.r1 and rs.r2 and (rs.node is None): rs.node = root return rs if __name__ == "__main__": tree = [15, 7, 30, 4, 13, 20, 35, 3, 6, 10, 14, None, None, None, None] nodes = [(6, 10), (4, 6), (7, 6), (3, 7), (3, 14), (6, 35)] root = create_binary_tree(tree) printBinaryTree(root) for n in nodes: p1 = TreeNode(n[0]) p2 = TreeNode(n[1]) node_rs = first_common_ancestor(root, p1, p2) print(f"p1 = {n[0]}, p2 = {n[1]}, common = {node_rs.val}")
root = None return child # has two children child = self.minChild(root.right) root.val = child.val root.right = self.deleteNode(root.right, child.val) return root def minChild(self, node: BinaryTreeNode) -> BinaryTreeNode: curr = node while curr.left is not None: curr = curr.left return curr t = create_binary_tree([5, 3, 7, 2, 4, 6]) print_binary_tree(t) solution = Solution() print('Search BST Recursively: ') ans = solution.searchBST1(t, 3) print_binary_tree(ans) print('Search BST Iteratively: ') ans = solution.searchBST2(t, 7) print_binary_tree(ans) print('Insert into BST Recursively: ') ans = solution.insertIntoBST1(t, 1) print_binary_tree(ans) print('Insert into BST Iteratively: ') ans = solution.insertIntoBST2(t, 8) print_binary_tree(ans)
queue = deque([root]) while queue: n = len(queue) level = [] for _ in range(n): root = queue.popleft() level.append(root.val) if root.left: queue.append(root.left) if root.right: queue.append(root.right) res.append(level) return res t = create_binary_tree([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print_binary_tree(t) solution = Solution() ans = solution.preorderTraversal1(t) print('Binary Tree Preorder Traversal Recursively: ', ans) ans = solution.preorderTraversal2(t) print('Binary Tree Preorder Traversal Iteratively: ', ans) ans = solution.inorderTraversal1(t) print('Binary Tree Inorder Traversal Recursively: ', ans) ans = solution.inorderTraversal2(t) print('Binary Tree Inorder Traversal Iteratively: ', ans) ans = solution.postorderTraversal1(t) print('Binary Tree Postorder Traversal Recursively: ', ans) ans = solution.postorderTraversal2(t) print('Binary Tree Postorder Traversal Iteratively: ', ans)
lca_left = self.lowestCommonAncestor2(root.left, p, q) lca_right = self.lowestCommonAncestor2(root.right, p, q) # process if lca_left is None: return lca_right if lca_right is None: return lca_left if lca_left and lca_right: return root # return return None solution = Solution() tree = create_binary_tree([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]) print_binary_tree(tree) p = cherry_pick(tree, 5) q = cherry_pick(tree, 1) ans = solution.lowestCommonAncestor1(tree, p, q) print('lowestCommonAncestor1 of', '(', p.val, ') and (', q.val, ') is (', ans.val, ')') ans = solution.lowestCommonAncestor2(tree, p, q) print('lowestCommonAncestor2 of', '(', p.val, ') and (', q.val, ') is (', ans.val, ')') p = cherry_pick(tree, 5) q = cherry_pick(tree, 4) ans = solution.lowestCommonAncestor1(tree, p, q) print('lowestCommonAncestor1 of', '(', p.val, ') and (', q.val, ') is (', ans.val, ')') ans = solution.lowestCommonAncestor2(tree, p, q) print('lowestCommonAncestor2 of', '(', p.val, ') and (', q.val, ') is (', ans.val, ')')