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. Java :构造器中的显式参数和this隐式参数

    1.构造器 写一个Java类,首先要先从构造器开始,构造器与类同名,在构造类的对象时会先从构造器开始. 构造器总是伴随着new操作符的执行而被调用. 构造器主要是用来初始化类的实例域. 构造器的特点: ...

  2. Java:静态内部类的使用目的、使用限制、与非静态内部类的对比

    Java之静态内部类(static class) 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static关键字修饰),也可以是非静态的. 一.静态内部类的使用目的. 在 ...

  3. 第三节课:简单的网络命令和ARP欺骗

    MTU :最大传输单元 RX:收包 TX:   发包 Ifconfig: IP配置命令,config是linux中用于显示或配置网络设备(网络接口卡)的命令 ifconfig eth0 192.168 ...

  4. 【二】刚学Python的几道简单练习题

    python交友娱乐会所:613176398 1.使用while循环输入 1 2 3 4 5 6     8 9 10 2.求1-100的所有数的和 3.输出 1-100 内的所有奇数 4.输出 1- ...

  5. 修改yum源

    安装 centos 之后,修改 yum 源到其它国内源 1. 备份原文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Ba ...

  6. oracle数据库使用心得之与SQL serve数据库的差异

    网上对于SQL数据库的使用比较详细,但是对于Oracle的使用比较少,本文特别适合学过SQL数据库但是工程需要使用Oracle数据的编程人员查看, 时间匆忙,文章可能写得不够详细,希望有人指出错误或者 ...

  7. 如何使用windows版Docker并在IntelliJ IDEA使用Docker运行Spring Cloud项目

    如何使用windows版Docker并在IntelliJ IDEA使用Docker运行Spring Cloud项目 #1:前提准备 1.1 首先请确认你的电脑是windows10专业版或企业版,只有这 ...

  8. hdu3756三分基础题

    Dome of Circus Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. Ubuntu中MongoDB安装

    在Ubuntu中MongoDB有时候启动不起来,可以参考以下方法从新安装: 1.导入包管理系统使用的公钥 Ubuntu 的软件包管理工具(即dpkg和APT)要求软件包的发布者通过GPG密钥签名来确保 ...

  10. MySQL Base

    /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 ---> input pwd /* 数据库存贮引擎 */    InnoDB :        1) ...