from functools import reduce


def extract_leaves(tree):
    """
    Extract the leaves of a plan decomposition tree in the Monroe corpus.
    Inputs:
       tree: the plan tree, of the form (node, subtree1, subtree2, ...)
            node is a grounded operator of the form (name, arg1, arg2, ...)
    Outputs:
        leaves[i]: The i^{th} leaf, also a grounded operator of the form (name, arg1, arg2, ...) 
    """
    if type(tree[0])==str: # base case, "tree" is a node
        return (tree,)
    else: # recursive case, tree is a tree, recurse on subtrees
        return reduce(lambda x,y: x+y, map(extract_leaves, tree[1:]))