字典的最大深度 python 实现
生活随笔
收集整理的這篇文章主要介紹了
字典的最大深度 python 实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
從本質(zhì)上講嵌套字典是一顆任意的多叉樹,可以參考我的博客如何求多叉樹的深度
#!/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)總結(jié)
以上是生活随笔為你收集整理的字典的最大深度 python 实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: N叉树的深度 python实现
- 下一篇: python os.system执行sh