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 ...
随机推荐
- Vim操作 -- 按列
1, 拷贝行 Y y 拷贝当前光标字符 如果需要拷贝整个单词,可以用 ye,e表示跳到词尾 2, 粘贴 P(大写) 粘贴到光标前 p(小写)粘贴到光标后 3, 进入快操作模式 ctrl+q 4, 用 ...
- some方法过滤
// 已经存在该tab时跳过 this.tabs.some(item => item.title === option.title) || this.tabs.push(option)
- System.Web.Mvc.ContentResult.cs
ylbtech-System.Web.Mvc.ContentResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Publ ...
- iOS之CGPath相关属性(一)
#ifndef CGPATH_H_ #define CGPATH_H_ForeverGuard博客园 #include <CoreFoundation/CFBase.h> #include ...
- Tree and Permutation (HDU 6446) 题解
// 昨天打了一场网络赛,表现特别不好,当然题目难度确实影响了发挥,但还是说明自己太菜了,以后还要多多刷题. 2018 CCPC 网络赛 I - Tree and Permutation 简单说明一下 ...
- selenium简单应用
文章引用自:https://wenku.baidu.com/view/d5c296c75727a5e9846a6182.html 例子:
- java连接SQLserver数据库模板代码
package cn.mldn.lxh.dbc; import java.sql.Connection; import java.sql.DriverManager; public class Dat ...
- 网络结构解读之inception系列五:Inception V4
网络结构解读之inception系列五:Inception V4 在残差逐渐当道时,google开始研究inception和残差网络的性能差异以及结合的可能性,并且给出了实验结构. 本文思想阐述不多, ...
- 1、mysql安装教程
1.https://www.runoob.com/mysql/mysql-install.html 参考安装链接 Windows 上安装 MySQL Windows 上安装 MySQL 相对来说会 ...
- 实现一个koa-logger中间件
//koa-logger.js module.exports = async(ctx,next)=>{ const start = new Date().getTime() // 中间件异步处理 ...