tree <- BalancedTree(7)
# Verify that node numbering increases away from root
plot(tree)
nodelabels()
# ListAncestors expects a tree in Preorder
tree <- Preorder(tree)
edge <- tree$edge
ancestors <- ListAncestors(edge[, 1], edge[, 2])
MRCA(1, 4, ancestors)
# If a tree must be in postorder, use:
tree <- Postorder(tree)
edge <- tree$edge
ancestors <- lapply(seq_len(max(edge)), ListAncestors,
parent = edge[, 1], child = edge[, 2])
Run the code above in your browser using DataLab