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 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.
对于每一个点,left 和 right depth 之和,就等于这个点的最大直径。换一句话说,就是这个点,左边能延伸出去最大值 + 右边能延伸出去的最大值,加一起,就等于这个点的左边最远的点到右边最远的点的距离。 就是最大直径。
关键点:用post order去返回每一个点的depth(在之前depth值上+1),null点就返回0(base case);
需要两个function,因为getDepth function 返回的都是depth,不是diameter,所以需要diameterOfBinaryTree 来单独返回diameter;
每一个点的最大直径等于左边的depth 加上 右边的depth,取所有点中最大的值。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = 0;
public int diameterOfBinaryTree(TreeNode root) {
getDepth(root);
return res;
}
public int getDepth(TreeNode root) {
if(root == null) return 0;
int l = getDepth(root.left);
int r = getDepth(root.right);
res = Math.max(res,l+r);
return Math.max(l,r) + 1;
}
}
543. Diameter of Binary Tree【Easy】【二叉树的直径】的更多相关文章
- 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 ...
- 14.Diameter of Binary Tree(二叉树的直径)
Level: Easy 题目描述: Given a binary tree, you need to compute the length of the diameter of the tree. ...
- [leetcode] 543. Diameter of Binary Tree (easy)
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...
- 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的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
- [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 二叉树的直径
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 二叉树的最大直径
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...
随机推荐
- 例子Architecting Android…The clean way?----代码分析
Presention层: 整个应用启动的时候,就执行依赖的初始化.编译项目之后,Dagger依赖框架使用ApplicationComponent生成一个DaggerApplicationCOmpo ...
- 判定对象是否存活的算法----GC_ROOT算法
要应用GC_ROOT算法,判定某个对象是否会被回收,关键是要确定root.确定root之后,你就可以根据代码绘制可达链,从而就可以进行分析了,分析哪些对象会被泄漏,哪些对象会被回收,如果GC执行的时候 ...
- 省队集训 Day4 a
[题目大意] 求有多少区间只包含1个出现次数为1的数. $1\leq n \leq 5*10^5, 0 \leq a_i \leq 10^9$ [题解] 考虑枚举右端点,设这个数上一次出现位置为pre ...
- chrome最小字体12px如何修改
在html标记样式里加入 <style> html { -webkit-text-size-adjust:none } </style> 这样的方式可以设置chrome字体小于 ...
- hdu 1869 六度分离(最短路floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 六度分离 Time Limit: 5000/1000 MS (Java/Others) M ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- ubuntu 下安装 activate-power-mode
转自网络 被朋友圈中的atom的activate-power-mode 震撼到了,于是想试试. 步骤如下 首先安装atom: sudo add-apt-repository ppa:webupd8te ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- linux系统下git使用
转载:http://www.cnblogs.com/bear2flymoon/p/4335364.html?ADUIN=563508762&ADSESSION=1430887070&A ...
- FAN54015 充電電流 軟硬體設定
Ex1: Vrsense 選 37.4 mV --- 在第二張圖 Rsense 選 50 mΩ --- 在第三張圖 37.4 / 50 = 748 mA Ex2: Vrsense 選 44.2 mV ...