from implementations.tree import BinaryTreeNode def binary_tree_paths(root): return list(_binary_tree_paths(root)) def _binary_tree_paths(root): if root.left: for path in _binary_tree_paths(root.left): yield '%s->%s' % (root.value, path) if root.right: for path in _binary_tree_paths(root.right): yield '%s->%s' % (root.value, path) if root.left is None and root.right is None: yield root.value if __name__ == '__main__': tree = BinaryTreeNode(4) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(7) tree.right.left = BinaryTreeNode(6) tree.right.right = BinaryTreeNode(9) print binary_tree_paths(tree)
from implementations.tree import BinaryTreeNode def vertical_traversal(root): cache = defaultdict(list) queue = deque([(0, root)]) min_level = 0 while len(queue) > 0: level, curr = queue.popleft() min_level = min(min_level, level) cache[level].append(curr.value) if curr.left is not None: queue.append((level - 1, curr.left)) if curr.right is not None: queue.append((level + 1, curr.right)) result = [] while min_level in cache: result.append(cache[min_level]) min_level += 1 return result if __name__ == '__main__': tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(9) tree.right = BinaryTreeNode(20) tree.right.left = BinaryTreeNode(15) tree.right.right = BinaryTreeNode(7) print vertical_traversal(tree)
1 / \ 2 2 \ \ 3 3 """ from implementations.tree import BinaryTreeNode def symmetric(tree): return _symmetric(tree.left, tree.right) def _symmetric(tree1, tree2): if tree1 is None and tree2 is None: return True elif tree1 is None or tree2 is None: return False return tree1.value == tree2.value and _symmetric(tree1.left, tree2.right) and _symmetric(tree1.right, tree2.left) if __name__ == '__main__': tree = BinaryTreeNode(1) tree.left = BinaryTreeNode(4) tree.left.left = BinaryTreeNode(3) tree.left.right = BinaryTreeNode(2) tree.right = BinaryTreeNode(4) tree.right.left = BinaryTreeNode(2) tree.right.right = BinaryTreeNode(3) print symmetric(tree)
""" Write a function to check if two trees have the same structure and the same elements. """ from implementations.tree import BinaryTreeNode def equal(tree1, tree2): if tree1 is None and tree2 is None: return True elif tree1 is None or tree2 is None: return False return tree1.value == tree2.value and equal(tree1.left, tree2.left) and equal(tree1.right, tree2.right) if __name__ == "__main__": tree1 = BinaryTreeNode(4) tree1.left = BinaryTreeNode(2) tree1.left.left = BinaryTreeNode(1) tree1.right = BinaryTreeNode(7) tree1.right.left = BinaryTreeNode(6) tree1.right.right = BinaryTreeNode(9) tree2 = BinaryTreeNode(4) tree2.left = BinaryTreeNode(2) tree2.left.left = BinaryTreeNode(1) tree2.right = BinaryTreeNode(7) tree2.right.left = BinaryTreeNode(6) tree2.right.right = BinaryTreeNode(9) print equal(tree1, tree2)
""" from implementations.tree import BinaryTreeNode def max_depth(tree): if not tree: return 0 stack = [(1, tree)] max_h = 1 while stack: depth, node = stack.pop() if node.left: stack.append((depth + 1, node.left)) if node.right: stack.append((depth + 1, node.right)) if depth > max_h: max_h = depth return max_h if __name__ == '__main__': t = BinaryTreeNode(5) t.left = BinaryTreeNode(2) t.right = BinaryTreeNode(3) t.left.left = BinaryTreeNode(9) t.left.left.left = BinaryTreeNode(12) t.right.left = BinaryTreeNode(-1) t.right.right = BinaryTreeNode(10) print max_depth(t)