[LC] 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 binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1
/ \
2 3
/ \
4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res;
public int diameterOfBinaryTree(TreeNode root) {
helper(root);
return res;
} private int helper(TreeNode root) {
if (root == null) {
return 0;
}
int left = helper(root.left);
int right = helper(root.right);
res = Math.max(res, left + right);
return left > right ? left + 1 : right + 1;
}
}
[LC] 543. Diameter of Binary Tree的更多相关文章
- 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 ...
- 【leetcode_easy】543. Diameter of Binary Tree
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
- 543. Diameter of Binary Tree 二叉树的最大直径
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...
- 543. Diameter of Binary Tree
https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...
- 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&Python] Problem 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]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【Easy】【二叉树的直径】
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 二叉树的直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
随机推荐
- STL学习顺序
仅供参考,大家要学会自己制作,很有成就感的啊! 先看<c++标准程序库>这本书很厚,但是其实很简单,然后再看effective stl,注意顺序,我初学的时候stl还没了解多少就看effe ...
- FFmpeg命令大全(更新中)
1.视频抽取音频: ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aacffmpeg -i 3.mp4 -vn -y -acodec copy 3.m4a
- swoole怎么连接数据库
异步连接到MySQL服务器. $serverConfig为MySQL服务器的配置,必须为关联索引数组 $callback连接完成后回调此函数 swoole连接数据库实例: 推荐学习:swoole教程 ...
- Java模板引擎之Freemarker 学习笔记 一
什么是Freemarker Freemarker是模板引擎,不是Web框架,只是视图层的组件,官网是 https://freemarker.apache.org/ Freemarker原理 数据模型+ ...
- FZU_1683 矩阵快速幂 求和
这个题目确实是很简单的一个矩阵快速幂,但是我在求和的时候,用的是标准的求和,即,一共计算logN次Ak,但是这样会超时. 后来就发现原来本身和Sn=Sn-1+Fn:即Sn本身可以写在矩阵当中,所以直接 ...
- css块级元素
<CSS权威指南>中文字显示:任何不是块级元素的可见元素都是内联元素.其表现的特性是“行布局”形式,这里的“行布局”的意思就是说其表现形式始终以行进行显示.比如,我们设定一个内联元素bor ...
- MySQL--SQL分类
SQL语句主要可以划分为以下3个类别: DDL(Data Definition Languages)语句:数据定义语言,这些语句定义了不同的数据段.数据库.表.列.索引等数据库对象. 常用的语句关键字 ...
- Serverless 公司的远程团队沟通策略
本文系译文,Serverless 团队分散在全球各地,本文介绍我们如何管理沟通策略和远程协作. 原作者:FelixDesroches 译者:Aceyclee 首先向不了解我们的人说明一下,Server ...
- Channels(纪念一下卡我心态的一道题)
链接:https://ac.nowcoder.com/acm/contest/3947/C来源:牛客网 题目描述 Nancy喜欢学习,也喜欢看电视. 为了想了解她能看多长时间的节目,不妨假设节目从时刻 ...
- 吴裕雄--天生自然ShellX学习笔记:Shell 流程控制
和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?php if (isset($_GET["q"])) { search(q); ...