博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.1 - 递归练习题
阅读量:4519 次
发布时间:2019-06-08

本文共 1130 字,大约阅读时间需要 3 分钟。

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))

 

转载于:https://www.cnblogs.com/alice-bj/p/8449377.html

你可能感兴趣的文章
给datalist加自动编号(解决博客的第XX楼)
查看>>
BZOJ3282: Tree (LCT模板)
查看>>
ES6中变量的解构赋值
查看>>
编译器C-Free V352注册算法分析
查看>>
数据绑定控件Reperter
查看>>
【codeforces】【比赛题解】#937 CF Round #467 (Div. 2)
查看>>
剑指Offer学习笔记(3)——解决面试题的思路
查看>>
.NET Framework基础知识(二)(转载)
查看>>
Yii DataProvider
查看>>
BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
查看>>
hdu 1114 Piggy-Bank
查看>>
maven集成tomcat插件启动报错
查看>>
Boost库编译安装
查看>>
Python matplot画散列图
查看>>
C#/.NET整数的三种强制类型转换(int)、Convert.ToInt32()、int.Parse()的区别
查看>>
算法复习——数位dp(不要62HUD2089)
查看>>
PhpSpreadsheet如何读取excel文件
查看>>
如何选购一款好的人事档案管理系统
查看>>
Spark2.1.0——运行环境准备
查看>>
[转载]C#异步调用四大方法详解
查看>>