def func(target, dict):
if not dict:
return []
for key , value in dict.items():
if key == target:
return [key] #return list with single element as key
returnValue = func(target , value)
if returnValue:
return [key] + returnValue ; #append key to returnValue
return []
inpDict = {
'1' : {'2':{}},
'3' : {'4':{},'5':{}},
'6': {'7':{'8':{'9':{},'10':{}}}},
'11':{}
}
print(func('5',inpDict))
ZGVmIGZ1bmModGFyZ2V0LCBkaWN0KToKCWlmIG5vdCBkaWN0OgoJCXJldHVybiBbXQoJZm9yIGtleSAsIHZhbHVlIGluIGRpY3QuaXRlbXMoKToKCQlpZiBrZXkgPT0gdGFyZ2V0OgoJCQlyZXR1cm4gW2tleV0gI3JldHVybiBsaXN0IHdpdGggc2luZ2xlIGVsZW1lbnQgYXMga2V5CgkJcmV0dXJuVmFsdWUgPSBmdW5jKHRhcmdldCAsIHZhbHVlKQoJCWlmIHJldHVyblZhbHVlOgoJCQlyZXR1cm4gW2tleV0gKyByZXR1cm5WYWx1ZSA7ICNhcHBlbmQga2V5IHRvIHJldHVyblZhbHVlCglyZXR1cm4gW10KCQppbnBEaWN0ID0gewoJJzEnIDogeycyJzp7fX0sCgknMycgOiB7JzQnOnt9LCc1Jzp7fX0sCgknNic6IHsnNyc6eyc4Jzp7JzknOnt9LCcxMCc6e319fX0sCgknMTEnOnt9Cn0KcHJpbnQoZnVuYygnNScsaW5wRGljdCkp