zoukankan      html  css  js  c++  java
  • 2.1

     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 否则返回False
    28 def func(menu, name):
    29     for con in menu:
    30         if name != con['text']:
    31             if func(con['children'], name) == True:
    32                 return True
    33             else:
    34                 func(con['children'], name)
    35         else:
    36             print(con['text'])
    37             return True
    38     else:
    39         return False
    40 
    41 name = input("输入节点名字:")
    42 print(func(menu, name))
  • 相关阅读:
    ADHOC Report 配置
    html tags
    Stingray验证机制
    常用jQuery知识
    Communication API
    stingray前端架构总体设计及运行过程
    REP report开发技巧
    WorkFlow业务介绍
    MySQL auto_increment初始值设置
    SQL Server中order by的使用,我们来填坑
  • 原文地址:https://www.cnblogs.com/alice-bj/p/8449377.html
Copyright © 2011-2022 走看看