[leetcode]543. Diameter of Binary Tree二叉树的直径
题目中的直径定义为:
任意两个节点的最远距离
没想出来,看的答案
思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1))
遍历并更新结果
int res = 1;
public int diameterOfBinaryTree(TreeNode root) {
helper(root);
return res-1;
}
public int[] helper(TreeNode root)
{
//两个量分别是节点深度,节点最大diameter
if (root==null) return new int[]{0,0};
int cur = 0;
int[] l = helper(root.left);
int[] r = helper(root.right);
cur = Math.max(Math.max(l[1],r[1]),l[0]+r[0]+1);
res = Math.max(res,cur);
return new int[]{Math.max(l[0],r[0])+1,cur};
}
[leetcode]543. 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 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 ...
- [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 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ...
- 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 ...
- 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二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它 ...
- [leetcode] 543. Diameter of Binary Tree (easy)
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...
随机推荐
- guava中的SettableFuture分析
当缓存中没有要找的数据时,则要从数据库中去查询,而当并发量比较大时可能会击穿数据库,所以guava cache对同一值的查询做了合并请求的处理.其中就用到了SettableFuture,类似一把锁,只 ...
- 自学linux——18.FTP服务器的搭建
Centos7下FTP服务器的搭建 一.FTP的作用 文件传输协议(File Transfer Protocol,FTP),是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用 20. ...
- fist-第四天冲刺随笔
这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...
- 《高并发下的.NET》第2季 - 故障公告:高并发下全线崩溃
大家好,非常抱歉,在昨天下午(12月3日)的访问高峰,园子迎来更高的并发,在这样的高并发下,突发的数据库连接故障造成博客站点全线崩溃,由此给您带来很大的麻烦,请您谅解. 最近,我们一边在忙于AWS合作 ...
- 20200416_Centos 7.2 在安装系统之前把数据备份出来
第一种方法: 起因: 服务器强制断电, 然后就再也启动不起来了, 进入救援模式, 一直卡死在 charoot /mnt/sysimages; 然后想在救援模式下的shell中挂载我的2T的移动硬盘, ...
- XDown单文件版 下载工具 支持磁力等多种链接方式下载
原来的程序不带剪辑板探测,不支持迅雷链接等 增加功能后优化制作单文件版本. 下载类型为下图 magnet:?xt=urn:btih:836A228D932EF1C7EA1DD99D5D80B7CB0C ...
- 理解js浅拷贝和深拷贝
理解深拷贝和浅拷贝之前先了解下js中的基本类型和引用类型 1.基本类型: 在js中,数据的基本类型undefined,null,string,number,boolean,在变量中赋的实际值,基本类型 ...
- P1654 OSU! 题解
\(x\) 为该位置有 \(1\) 的期望. 统计两个值 : \(suma\) 和 \(sumb\). \(suma\) 表示连续 \(X\) 个 \(1\) , \(X\) 的平方的期望, \(su ...
- redis学习之——主从复制(replication)
准备:拥有linux环境,并安装redis mater:主机,进行写操作 slave:从机,进行读操作 一.配置 继续前边的学习.我们是拷贝redis.conf,文件到了/root /redis 下. ...
- redis 常用基本命令
redis 常用基本命令 redis-cli 启动set 键 值 # 存储 单条数据 # set 'zsj' 'bab' get 键 # 通过键获取值 # get ...