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 ...
随机推荐
- 02_mybatis开发dao的方法
MyBatis开发dao的方法 1. SqlSession使用范围 1.1 SqlSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂Sql ...
- Spring 声明式事务管理(11)
案例分析 本案例是图书管理系统精简部分,在数据库中有3张表.分别保存图书库存.图书信息和用户信息.下面是建表SQL语句 1 DROP TABLE IF EXISTS store; 2 DROP TAB ...
- spark jdk8 单词统计示例
在github上有spark-java8 实例地址: https://github.com/ypriverol/spark-java8 https://github.com/ihr/java8-spa ...
- mount: you must specify the filesystem type
最近工作中遇到一个问题,在linux mount /dev/vdb 到 /home 分区时报错: 1 2 # mount /dev/vdb /home mount: you must specif ...
- GridView编辑删除
A前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...
- 不用winio直接用c#函数实现模拟键盘
原理来自: http://blog.sina.com.cn/s/blog_71921a8e0100olaw.html /// <summary> /// 导入模拟键盘的方法 /// &l ...
- day 42 01--CSS的引入方式及CSS选择器
01--CSS的引入方式及CSS选择器 本节目录 一 CSS介绍 二 行内样式 三 内接样式 四 外接样式 五 CSS的选择器 六 CSS的高级选择器 七 CSS的属性选择器 八 CSS的伪类选择 ...
- 《DSP using MATLAB》Problem 8.29
来汉有一月,往日的高温由于最近几个台风沿海登陆影响,今天终于下雨了,凉爽了几个小时. 接着做题. %% ------------------------------------------------ ...
- Microsoft store打不开,解决办法
1.打开电脑,点击左下角的图标开始,然后找到设置选项,也可以直接使用快捷键win+i: 2.在弹出的新页面中有很多选项功能,找到并且点击”网络和Internet“选项: 3.查看网络连接方式,如果是宽 ...
- Unity IoC Base On MVC
Unity框架,是一个经典的IoC模式实现方式,其通过config文件配置section,将接口与实现解藕,config中的section配置的container以全名称对应,使得应用程序无需像Nin ...