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.


 题目标签:Tree
  这道题目给了我们一个二叉树,让我们找到二叉树的直径 - 最远的距离存在于两个点之间。 我们另外需要一个function - getDepth。 这个function利用post order,从最左边下面开始返回每一个点的depth, 如果这个点是null,那么返回0,依次像上一个level,每次加1。
在知道了每一个点的depth之后,我们可以来找到树的最大直径。我们来分析一下,怎么找到最大直径,取题目中给的例子,我们只看2 4 5 这基本结构, 2 4 5 这个子树的最大直径是2,这个直径等于 2的left 4的depth(1) 和 2的right 5的depth(1),两边的depth之和就等于最大直径 。 我们再回到原来的树立,1,2,3,4,5这个原题中的例子,我们从下往上看, 4的depth = 1, 5的depth = 1, 对于每一个parent node 取两个children的depth之和,和之前的diameter比较,大的就取代。 所以2的diameter = 2; 接着看2的depth = 2, 3的depth = 1, 那么1的diameter = 2 + 1  = 3, 比之前的diameter大,所以最大直径等于3。
 
对于每一个点,left 和 right depth 之和,就等于这个点的最大直径。换一句话说,就是这个点,左边能延伸出去最大值 + 右边能延伸出去的最大值,加一起,就等于这个点的左边最远的点到右边最远的点的距离。 就是最大直径。
 
 

Java Solution:

Runtime beats 72.26%

完成日期:06/30/2017

关键词:Tree

关键点:用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; }
* }
*/
public class Solution
{
int diameter = 0; public int diameterOfBinaryTree(TreeNode root)
{
getDepth(root); return diameter;
} public int getDepth(TreeNode root)
{
if(root == null)
return 0; int left = getDepth(root.left);
int right = getDepth(root.right); int temp = left + right; if(temp > diameter)
diameter = temp; return Math.max(left, right) + 1;
}
}

参考资料:

http://blog.csdn.net/zhouziyu2011/article/details/64123326

改动了一下,因为temp = left + right + 2 有点绕。返回0更直接易懂。

 
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
 

LeetCode 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 ...

  2. 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 ...

  3. [leetcode]543. Diameter of Binary Tree二叉树的直径

    题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 ...

  4. [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 ...

  5. 543 Diameter of Binary Tree 二叉树的直径

    给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树          1         / \        2 ...

  6. [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 ...

  7. 543. Diameter of Binary Tree 二叉树的最大直径

    [抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...

  8. Leetcode543.Diameter of Binary Tree二叉树的直径

    给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2    3 / \ 4  5 返回 3, 它 ...

  9. [leetcode] 543. Diameter of Binary Tree (easy)

    原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...

随机推荐

  1. SparkStreming之updateStateByKey

    正文 上一篇简单的写了一个socketTextStream的demo,这个问题就是每一次不能将之前和之后的数据进行合并统一.接下来我们通过demo进行把着这个问题解决. val conf = new ...

  2. 巧用 BootStrap --- 栅格系统(布局)轻松搞定网页响应式布局!

    摘要:Bootstrap 为我们提供了一套响应式.移动设备优先的流式栅格系统,合理的使用栅格系统将会使得网站页面布局变得更加简单,在设置了媒体查询之后,响应式网站也无需再单独写了.接下来我以Boots ...

  3. JMeter关联(正则表达式提取器)

    关联:与系统交互过程中,系统返回的内容,需要在接下来的交互中用到,如防止csrf攻击而生成的token. 从前一个请求中取,用Regular Expression Extractor 正则表达式提取器 ...

  4. P1050

    问题 F: P1050 时间限制: 1 Sec  内存限制: 128 MB提交: 37  解决: 27[提交][状态][讨论版] 题目描述 一个字符串A的子串被定义成从A中顺次选出若干个字符构成的串. ...

  5. S2_OOP第三章

    第一章 多态 概念 多态是具有表现多种型生态的能力的特征,同一个实现接口,使用不同的实例而执行不同的操作 子类转换父类(向上转型) 用父类接受子类,向上转型 向上转型的规则: 讲一个父类的引用志向一个 ...

  6. 架构师之路-在Dubbo中开发REST风格的远程调用

    架构师之路:从无到有搭建中小型互联网公司后台服务架构与运维架构 http://www.roncoo.com/course/view/ae1dbb70496349d3a8899b6c68f7d10b 概 ...

  7. python之爬虫

    一.从网页爬下字符串清除特殊字符 import re def validateTitle(title): rstr = r"[\/\\\:\*\?\"\<\>\|\t] ...

  8. 一些实用的JQuery代码片段收集(筛选,搜索,样式,清除默认值,多选等)

    //each遍历文本框 清空默认值 $(".maincenterul1").find("input,textarea").each(function () { ...

  9. WPF 验证没有通过无法保存数据(非常好)+ 虚似数据库

    Validation control with a single validation rule is easy, but what if we need to validate a control ...

  10. python concurrent.futures

    python因为其全局解释器锁GIL而无法通过线程实现真正的平行计算.这个论断我们不展开,但是有个概念我们要说明,IO密集型 vs. 计算密集型. IO密集型:读取文件,读取网络套接字频繁. 计算密集 ...