code: def filter_tree(condition, tree):
      
      if condition(tree):
          for node in tree.nodes:
              # this works in python > 3.3
              # yield from filter_tree(condition, node)
              for n in filter_tree(condition, node):
                  yield n
      yield tree
docs: Return parts of the tree that fulfill condition