14.Diameter of Binary Tree(二叉树的直径)
Level:
Easy
题目描述:
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.
思路分析:
题目要求找出二叉树的直径,二叉树的直径的定义是,任意两节点之间的最长路径,这个路径不一定会经过根节点。我们的解题思路是,最长路径肯定是以某个节点为中转,分别从这个节点的左右子节点延伸到叶子节点的两条无转向的路径和。所以只需要求出所有节点作为中转节点的路径长度,取其中最大的就得到了答案。
代码:
public class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x){
val=x;
}
}
public class Solution{
public int diameterOfBinaryTree(TreeNode root){
if(root==null)
return 0;
int res=depth(root.left)+depth(root.right);
int ledia=diameterOfBinaryTree(root.left);
int ridia=diameterOfBinaryTree(root.right);
return Math.max(Math.max(res,ledia),ridia);
}
public int depth(TreeNode root){
if(root==null)
return 0;
int left=depth(root.left);
int right=depth(root.right);
return Math.max(left,right)+1;
}
}
14.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 ...
- Leetcode543.Diameter of Binary Tree二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它 ...
- [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 ...
- 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 ...
随机推荐
- laravel4 composer报错 d11wtq/boris v1.0.10 requires ext-pcntl
laravel4 composer报错 d11wtq/boris v1.0.10 requires ext-pcntl laravel 4.2 用composer 安装任何包都会报这个错,通过谷歌找到 ...
- 将本地代码上传至github
注册github账号 https://github.com/ 安装git工具 https://git-for-windows.github.io 1.在github中创建一个项目 2.填写相应信息,点 ...
- leetcode241
public class Solution { public IList<int> DiffWaysToCompute(string input) { List<int> re ...
- iOS多线程各种安全锁介绍 - 线程同步
一.atomic介绍 github对应Demo:https://github.com/Master-fd/LockDemo 在iOS中,@property 新增属性时,可以增加atomic选项,ato ...
- Android Studio Build APK没有报错,但是Generate signed apk报错
有时候 ,我们在调试APK,直接Build是可以正常生成,没有报错,但是当我们将自己的签名文件加上去,就会报错.一般情况下,我们可以在build.gradle中的android{}里面添加一个东西 l ...
- MySQL 删除字段数据某关键字后的所有数据
),'开发商') WHERE Compay LIKE '%开发商%'; sql附上
- mysql:mysql Access denied for user root@
最近用本地Navicat连接集群的mysql,报了上述的错误,我认为是权限问题 之前试过赋权限给所有人,但是我这边还是连接不上,无奈,试试只分给我一个IP 开始:mysql -uroot -p //先 ...
- 一个servlet处理多个请求(使用Method的反射机制)
方法一 可以通过在请求的时候加上参数,然后在servlet中获取请求的参数,再去调用对应的方法.达到一个servlet处理多个请求的目的 test.jsp: <%@ page language= ...
- os模块 os.stat('path/filename') os.path.dirname(path) os.path.exists(path) os.path.join(path1[, path2[, ...]])
提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于 ...
- REST API (更新文档)
Elasticsearch的更新文档API准许通过脚本操作来更新文档.更新操作从索引中获取文档,执行脚本,然后获得返回结果.它使用版本号来控制文档获取或者重建索引. 我们新建一个文档: 请求:PUT ...