UUBlog

UUBlog

  • Home
  • Categories32
  • Archives124
  • Tags123
  • About
  • Search
  • Table of Contents
  • Overview

尹安灿

124 posts
32 categories
123 tags
GitHub

Python求二叉树最大深度

Posted on 2017-11-09 Edited on 2024-02-03 In python , algorithms

继前面Python二叉树递归和遍历

二叉树图

binary tree

1
2
3
4
5
6
def max_depth(node):
if not node:
return 0
ldepth = max_depth(node.left)
rdepth = max_depth(node.right)
return max(ldepth,rdepth) + 1

结果:4

关注公众号 尹安灿

# python # binary tree # max depth
Crontab执行命令失败 手工执行成功
Linux挂载NTFS但是不可写 Read-only
粤ICP备17061759号
© 2024 尹安灿
Powered by Hexo & NexT.Gemini
0%