[leetcode]543. Diameter of Binary Tree二叉树的直径
2024-10-18 19:30:11
题目中的直径定义为:
任意两个节点的最远距离
没想出来,看的答案
思路是: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};
}
最新文章
- oracle触发器
- linux下安装 oracle 11g
- CentOS 6.4 查看每个进程的网络流量
- input输入子系统
- js笔记---(运动)通用的move方法,兼容透明度变化
- Linux Shell编程(22)——时间/日期 命令
- Apache Commons Pool 故事一则
- 经典排序算法 - 基数排序Radix sort
- [品质生活] 舒适 Schick HYDRO 5剃须刀
- Electron 桌面应用打包(npm run build)简述(windows + mac)
- JAVA和Android的回调机制
- java 自动化测试平台构建思想
- SpringBoot 读取properties配置文件 @Value使用 中文乱码问题
- mybatis异常:Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for。。。。。。
- Head First Servlets &; JSP 学习笔记 第一章 —— 前言和体系结构
- XSS详解
- swift - UISwitch 的用法
- PHP 成长规划
- 7、ORM
- 教你阅读MSDN英文文档,迅速提升编程能力
热门文章
- Apache Beam,批处理和流式处理的融合!
- LeetCode 044 Wildcard Matching
- 冲刺随笔——Day_Nine
- 20200513_安装windows sql server 2012 _ ws功能 NetFx3时出错,错误代码:-2146498298
- JMeter 安装 启动(即中文的修改)
- MongoEngine模块
- PyQt学习随笔:Qt中tem Views(Model-Based)和Item Widgets(Item-Based)控件的用途和关系
- PyQt(Python+Qt)学习随笔:QAbstractScrollArea的viewPort(视口)理解
- PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象dockNestingEnabled属性
- Bootstrap 的基本使用