1 # 递归练习题 -- 深度查询 -- 不知道有多少层,使用递归 2 # 1.打印所有的节点text 3 # 2.输入一个节点名字,去遍历找,找到就打印,返回True 否则返回False 4 5 menu = [ 6 { 'text': '北京', 'children': [ 7 { 'text': '朝阳', 'children': []}, 8 { 'text': '昌平', 'children': [ 9 { 'text': '沙河', 'children': []},10 { 'text': '回龙观', 'children': []}11 ]}12 ]},13 { 'text': '上海', 'children': [14 { 'text': '宝山', 'children': []},15 { 'text': '金山', 'children': []}16 ]}17 ]18 19 # 1.打印所有的节点text 20 def func(m):21 for con in m:22 print(con['text'])23 func(con['children'])24 25 func(menu)26 27 # 2.输入一个节点名字,去遍历找,找到就打印,返回True 否则返回False28 def func(menu, name):29 for con in menu:30 if name != con['text']:31 if func(con['children'], name) == True:32 return True33 else:34 func(con['children'], name)35 else:36 print(con['text'])37 return True38 else:39 return False40 41 name = input("输入节点名字:")42 print(func(menu, name))