Binary Tree Flashcards

1
Q

Lowest Common Ancestor of nodes

A
def lca(root, nodes):
    if root is None: return None
    if root in nodes: return root
    left_lca = lca(root.left, nodes)
    right_lca = lca(root.right, nodes)
    if left_lca is None: return right_lca
    if right_lca is None: return left_lca
    return root
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

BFS (pre-order traversal) of binary tree

A

row = [root]
while row:
row = [child for n in row for child in (n.left, n.right) if child]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly