题目中的直径定义为:

任意两个节点的最远距离

没想出来,看的答案

思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1))

遍历并更新结果

int res = 1;
public int diameterOfBinaryTree(TreeNode root) {
helper(root);
return res-1;
}
public int[] helper(TreeNode root)
{
//两个量分别是节点深度,节点最大diameter
if (root==null) return new int[]{0,0};
int cur = 0;
int[] l = helper(root.left);
int[] r = helper(root.right);
cur = Math.max(Math.max(l[1],r[1]),l[0]+r[0]+1);
res = Math.max(res,cur);
return new int[]{Math.max(l[0],r[0])+1,cur};
}

最新文章

  1. oracle触发器
  2. linux下安装 oracle 11g
  3. CentOS 6.4 查看每个进程的网络流量
  4. input输入子系统
  5. js笔记---(运动)通用的move方法,兼容透明度变化
  6. Linux Shell编程(22)——时间/日期 命令
  7. Apache Commons Pool 故事一则
  8. 经典排序算法 - 基数排序Radix sort
  9. [品质生活] 舒适 Schick HYDRO 5剃须刀
  10. Electron 桌面应用打包(npm run build)简述(windows + mac)
  11. JAVA和Android的回调机制
  12. java 自动化测试平台构建思想
  13. SpringBoot 读取properties配置文件 @Value使用 中文乱码问题
  14. mybatis异常:Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for。。。。。。
  15. Head First Servlets & JSP 学习笔记 第一章 —— 前言和体系结构
  16. XSS详解
  17. swift - UISwitch 的用法
  18. PHP 成长规划
  19. 7、ORM
  20. 教你阅读MSDN英文文档,迅速提升编程能力

热门文章

  1. Apache Beam,批处理和流式处理的融合!
  2. LeetCode 044 Wildcard Matching
  3. 冲刺随笔——Day_Nine
  4. 20200513_安装windows sql server 2012 _ ws功能 NetFx3时出错,错误代码:-2146498298
  5. JMeter 安装 启动(即中文的修改)
  6. MongoEngine模块
  7. PyQt学习随笔:Qt中tem Views(Model-Based)和Item Widgets(Item-Based)控件的用途和关系
  8. PyQt(Python+Qt)学习随笔:QAbstractScrollArea的viewPort(视口)理解
  9. PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象dockNestingEnabled属性
  10. Bootstrap 的基本使用