def dfs(nodes):
    if nodes is not None:
        for node in nodes:
            yield node
for child in dfs(node['children']):
    yield child
for node in dfs(tree):
    if node['id'] in selected:
        node['selected'] = true
Обход и изменение древовидного списка структуры dict