node.left = TreeNode(int(nodes[index])) queue.append(node.left) index += 1 if nodes[index] is not '#': node.right = TreeNode(int(nodes[index])) queue.append(node.right) index += 1 return root if __name__ == "__main__": codec = Codec2() values = [] root = make_binary_tree(values) print(codec.serialize(root)) print(tree2list(codec.deserialize(codec.serialize(root)))) values = [1] root = make_binary_tree(values) print(codec.serialize(root)) print(tree2list(codec.deserialize(codec.serialize(root)))) values = [1, 2] root = make_binary_tree(values) print(codec.serialize(root)) print(tree2list(codec.deserialize(codec.serialize(root)))) values = [1, 2, 3, None, None, 4, 5] root = make_binary_tree(values)
if not root: return 0 lengths = [] def dfs(node): if not node: return -1 left = dfs(node.left) right = dfs(node.right) if node.left and node.left.val != node.val: left = -1 if node.right and node.right.val != node.val: right = -1 lengths.append(left + right + 2) return max(left, right) + 1 dfs(root) return max(lengths) if __name__ == "__main__": tree = [5, 4, 5, 1, 1, None, 5] root = make_binary_tree(tree) print(first_solution(root)) tree = [1, 4, 5, 4, 4, None, 5] root = make_binary_tree(tree) print(first_solution(root))
node1.val += node2.val node1.left = merge(node1.left, node2.left) node1.right = merge(node1.right, node2.right) return node1 return merge(t1, t2) def third_solution(t1: TreeNode, t2: TreeNode) -> TreeNode: if t1 and t2: node = TreeNode(t1.val + t2.val) node.left = third_solution(t1.left, t2.left) node.right = third_solution(t1.right, t2.right) return node else: return t1 or t2 if __name__ == "__main__": tree1 = [1, 3, 2, 5, None, None, None] tree2 = [2, 1, 3, None, 4, None, 7] root1 = make_binary_tree(tree1) root2 = make_binary_tree(tree2) print(tree2list(third_solution(root1, root2))) tree1 = [1, 2, None, 3] tree2 = [1, None, 2, None, 3] root1 = make_binary_tree(tree1) root2 = make_binary_tree(tree2) print(tree2list(third_solution(root1, root2)))
searched = pre_order(root.right, searched) return searched def in_order(root: TreeNode, searched=[]) -> List: if not root: return searched searched = in_order(root.left, searched) searched.append(root.val) searched = in_order(root.right, searched) return searched def post_order(root: TreeNode, searched=[]) -> List: if not root: return searched searched = post_order(root.left, searched) searched = post_order(root.right, searched) searched.append(root.val) return searched if __name__ == "__main__": values = ['F', 'B', 'G', 'A', 'D', None, 'I', None, None, 'C', 'E', 'H'] tree = make_binary_tree(values) print(pre_order(tree)) print(in_order(tree)) print(post_order(tree))
import heapq, collections, sys from tree import TreeNode, make_binary_tree, tree2list def first_solution(root: TreeNode) -> int: queue = collections.deque([root]) heap = [] while queue: node = queue.popleft() if not node: continue heapq.heappush(heap, node.val) queue.append(node.left) queue.append(node.right) min_diff = sys.maxsize cur_val = heapq.heappop(heap) while heap: next_val = heapq.heappop(heap) min_diff = min(min_diff, next_val - cur_val) cur_val = next_val return min_diff if __name__ == "__main__": values = [4, 2, 6, 1, 3, None, None] print(first_solution((make_binary_tree(values))))
else: return node.val if not root: return root root.val = dfs(root, root.val + 1) #root.left = first_solution(root.left) #root.left = first_solution(root.left) return root """ val = [0] def second_solution(root: TreeNode) -> TreeNode: if root: second_solution(root.right) val[0] += root.val root.val = val[0] second_solution(root.left) return root if __name__ == "__main__": null = None values = [4, 1, 6, 0, 2, 5, 7, null, null, null, 3, null, null, null, 8] sum_tree = second_solution(make_binary_tree(values)) print(tree2list(sum_tree))