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 longest path 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 {
public int diameterOfBinaryTree(TreeNode root) {
int[] globalMax = new int[];
helper(root, globalMax);
return globalMax[] - <= ? : globalMax[] - ;
} private int helper(TreeNode root, int[] globalMax) {
if (root == null) return ;
int leftMaxWithRoot = helper(root.left, globalMax);
int leftRightWithRoot = helper(root.right, globalMax); int maxWithRoot = Math.max(leftMaxWithRoot, leftRightWithRoot) + ;
globalMax[] = Math.max(globalMax[], leftMaxWithRoot + leftRightWithRoot + );
return maxWithRoot;
}
}
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——Diameter of Binary Tree
LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...
- 【leetcode_easy】543. Diameter of Binary Tree
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
- 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] 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 ...
- [Swift]LeetCode543. 二叉树的直径 | 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 - 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 ...
随机推荐
- rander()函数执行条件
调用this.setState({}),如果数据改变了,rander()就行执行
- Shell登陆
Shell登录信息 注:只对本地终端起作用,远程终端不起作用(也就是说这个文件对远程登录是无效的). 修改后: 输出: 注:这里在配置文件中添加\l之后会显示终端(这里为终端1),按住Alt+F2可以 ...
- centos后台运行python程序
在服务器上,为了退出终端,程序依然能够运行,需要设置程序在后台运行. 关键的命令:nohup *基本用法:进入要运行的py文件目录前 nohup python -u test.py > tes ...
- 19.Python转义字符及用法
在前面的章节中,我们曾经简单学习过转义字符,所谓转义,可以理解为“采用某些方式暂时取消该字符本来的含义”,这里的“某种方式”指的就是在指定字符前添加反斜杠 \,以此来表示对该字符进行转义. 举个例子, ...
- Android学习_Fragment
Fragment 使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理.从而可以更加方便的在运行过程中动态地更新Activity的用户界面.另外Fragment并不能单独 ...
- 开源!js实现微信/QQ直接跳转到支付宝APP打开口令领红包!附:demo
最近支付宝的领红包可真是刷爆了各个微信群啊,满群都是支付宝口令. 可是这样推广可不是办法,又要复制又要打开支付宝又要点领取,太麻烦了. 于是乎,提出了一个疑问!是否可以在微信里面点一个链接然后直接打开 ...
- MySql 时区错误
mysql的时区错误问题: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one ...
- springboot拦截异常信息发送邮件提醒
-- private JavaMailSender sender; 可能会出现注入错误,请注意yam配置文件中格式是否一致:否则会找不到注入的bean 一 发送邮件 在Springboot中发送邮件非 ...
- Mac平台最好用的万能开源免费播放器-IINA
1.安装 1)官网下载地址 https://iina.io/ 2)brew 方式安装 testdeMacBook-Pro:~ test$ brew cask install iina Updating ...
- 4.HadoopMapRe程序设计
1.介绍 2.基本构架与组件 3.基本算法