Leetcode543.Diameter of Binary Tree二叉树的直径
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。
示例 :
给定二叉树
1
/ \
2 3
/ \
4 5
返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。
注意:两结点之间的路径长度是以它们之间边的数目表示。
class Solution {
public:
int res = 0;
int diameterOfBinaryTree(TreeNode* root) {
if(root == NULL)
return 0;
res = max(res, GetDepth(root ->left) + GetDepth(root ->right) + 1);
diameterOfBinaryTree(root ->left);
diameterOfBinaryTree(root ->right);
return res - 1;
}
int GetDepth(TreeNode* root)
{
if(root == NULL)
return 0;
return 1 + max(GetDepth(root ->left), GetDepth(root ->right));
}
};
Leetcode543.Diameter of Binary Tree二叉树的直径的更多相关文章
- [LeetCode] 543. Diameter of Binary Tree 二叉树的直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- [LeetCode] Diameter of Binary Tree 二叉树的直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)
题目: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of ...
- 543 Diameter of Binary Tree 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ...
- [leetcode]543. Diameter of Binary Tree二叉树的直径
题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 ...
- [leetcode]543. Diameter of Binary Tree二叉树直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- 543. Diameter of Binary Tree 二叉树的最大直径
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...
- LeetCode543. Diameter of Binary Tree
Description Given a binary tree, you need to compute the length of the diameter of the tree. The dia ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
随机推荐
- Python flask 构建微电影视频网站✍✍✍
Python flask 构建微电影视频网站 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大 ...
- InfluxDB的安装和简介
InfluxDB简介 InfluxDB是一个时间序列数据库,旨在处理高写入和查询负载.它是TICK堆栈的组成部分 .InfluxDB旨在用作涉及大量带时间戳数据的任何用例的后备存储,包括DevOps监 ...
- UBOOT命令简写说明
所以命令都可以简写,只要命令前面的一部分不会跟其它命令相同,就可以不用写全整个命令. save 命令 CRANE2410 # sa Saving Environment to Flash... Un- ...
- 【HZOI2015】帕秋莉的超级多项式
题面 题目分析 超级模板题: 多项式乘法 多项式求逆 多项式开根 多项式求导 多项式求积分 多项式求对数 多项式求自然对数为底的指数函数 多项式快速幂 代码实现 #include<iostrea ...
- java.net.UnknownHostException 异常处理(转)
在linux系统下部署Java产品的集群环境时,后台报出如下异常,系统报找不到名为“QATest242”的主机: ERROR - Get local host name failed -com.tr ...
- 廖雪峰Java13网络编程-2Email编程-2接收Email
1接收Email协议类型 接收Email:收件人通过MUA软件把邮件从MDA抓取到本地计算机的过程. 1.1 POP3 从MUA到MDA使用最广泛的是协议是POP3 Post Office Proto ...
- ThinkPHP实现了ActiveRecords模式的ORM模型
ThinkPHP实现了ActiveRecords模式的ORM模型,采用了非标准的ORM模型:表映射到类,记录映射到对象.最大的特点就是使用方便和便于理解(因为采用了对象化),提供了开发的最佳体验,从而 ...
- Windows安全证书生成方法(开发者证书)
首先,查看本机安装的证书可在“运行”中输入:certmgr.msc 一.win8.8.1.win10系统,使用管理员powershell创建证书: (1)利用如下命令来创建证书并获取到其指纹 New- ...
- HTML 项目符号
无序符号 <ul> <li> </li> <li> </li> <li> </li> </ul> 属性 ...
- <b> <i> 标记
<b></b> bold 加粗 <i></i> italic 斜线 : 对应Word文档的 I 斜体 <u></u>unde ...