Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree

          1
/ \
2 3
/ \
4 5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def diameterOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.ans=1
def dfs(node):
if not node:
return 0
L=dfs(node.left)
R=dfs(node.right)
self.ans=max(self.ans,L+R+1)
return max(L,R)+1
dfs(root)
return self.ans-1

  

最新文章

  1. 有关servlet初学者的资源和建议
  2. 冒烟测试、α测试、Beta测试、性能测试
  3. CS=0xFFFF IP=0x0000与CS=F000 IP=FFF0
  4. ubuntu下提示/boot空间不足,解决办法
  5. 【异步编程】when.js
  6. Oracle 客户端配置笔记
  7. apollo实现c#与android消息推送(四)
  8. 【Flask】 项目结构说明
  9. Installing Supervisor and Superlance on CentOS
  10. CentOS6.5yum配置本地源
  11. CentOS 7 下安装oracle 11g碰到的一些问题
  12. Node_初步了解(3)回调,作用域,上下文
  13. python开启httpserver服务在自动化测试中的一个小运用
  14. zabbix 服务端安装(server)
  15. bzoj4176. Lucas的数论 杜教筛
  16. windows下的pycharm配置 linux环境
  17. mac电脑的系统偏好设置的安全与隐私的任何来源没有了
  18. 【linux之文件查看,操作,权限管理】
  19. 爬虫:Scrapy12 - Stats Collection
  20. 【刷题】BZOJ 1951 [Sdoi2010]古代猪文

热门文章

  1. Django信息安全相关之CSRF和XSS
  2. 利用ML&AI判定未知恶意程序——里面提到ssl恶意加密流检测使用N个payload CNN + 字节分布包长等特征综合判定
  3. windows7安装教程(vmware)
  4. Qt绘制文本一
  5. 命令创建Vue
  6. js 中class选择器,addClass,removeClass,hasClass,toggleClass,getByClass
  7. 个人前端学习路线图与github优秀前端开发者的路线图推荐
  8. java将字符串根据空格进行分割,使用split方法
  9. 《python》join、守护进程、锁/信号量/事件、进程队列
  10. Java基础知识补充