字典的最大深度 python 实现
生活随笔
收集整理的这篇文章主要介绍了
字典的最大深度 python 实现
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
从本质上讲嵌套字典是一颗任意的多叉树,可以参考我的博客如何求多叉树的深度
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed May 15 17:30:11 2019@author: lg """ d={'a':{'b':3,'c':{'d':4,'e':5}},'f':9}def maxDepth( root):if type(root)==int:return 0if type(root.values)==int:return 1return 1 + max(maxDepth(child) for child in root.values())p=maxDepth(d) print('字典的最大深度是: ',p)总结
以上是生活随笔为你收集整理的字典的最大深度 python 实现的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: N叉树的深度 python实现
- 下一篇: python os.system执行sh