def generate_phrases_by_dfs(node, level, phrase_level, result):
    """DFS a parsing tree and generate all phrases in the tree."""
    if len(node.child) == 0:
        # if current node is leaf
        word = node.value.strip()
        if level <= phrase_level:
            result.append(word)
        return word

    phrase = ""
    for child in node.child:
        phrase += " " + generate_phrases_by_dfs(child, level + 1, phrase_level, result)
    phrase = " ".join(phrase.split())

    if level == phrase_level:
        result.append(phrase)
    return phrase