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 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.
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 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二叉树的直径
题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是: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 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ...
- [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 ...
随机推荐
- Java :构造器中的显式参数和this隐式参数
1.构造器 写一个Java类,首先要先从构造器开始,构造器与类同名,在构造类的对象时会先从构造器开始. 构造器总是伴随着new操作符的执行而被调用. 构造器主要是用来初始化类的实例域. 构造器的特点: ...
- Java:静态内部类的使用目的、使用限制、与非静态内部类的对比
Java之静态内部类(static class) 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static关键字修饰),也可以是非静态的. 一.静态内部类的使用目的. 在 ...
- 第三节课:简单的网络命令和ARP欺骗
MTU :最大传输单元 RX:收包 TX: 发包 Ifconfig: IP配置命令,config是linux中用于显示或配置网络设备(网络接口卡)的命令 ifconfig eth0 192.168 ...
- 【二】刚学Python的几道简单练习题
python交友娱乐会所:613176398 1.使用while循环输入 1 2 3 4 5 6 8 9 10 2.求1-100的所有数的和 3.输出 1-100 内的所有奇数 4.输出 1- ...
- 修改yum源
安装 centos 之后,修改 yum 源到其它国内源 1. 备份原文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Ba ...
- oracle数据库使用心得之与SQL serve数据库的差异
网上对于SQL数据库的使用比较详细,但是对于Oracle的使用比较少,本文特别适合学过SQL数据库但是工程需要使用Oracle数据的编程人员查看, 时间匆忙,文章可能写得不够详细,希望有人指出错误或者 ...
- 如何使用windows版Docker并在IntelliJ IDEA使用Docker运行Spring Cloud项目
如何使用windows版Docker并在IntelliJ IDEA使用Docker运行Spring Cloud项目 #1:前提准备 1.1 首先请确认你的电脑是windows10专业版或企业版,只有这 ...
- hdu3756三分基础题
Dome of Circus Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Ubuntu中MongoDB安装
在Ubuntu中MongoDB有时候启动不起来,可以参考以下方法从新安装: 1.导入包管理系统使用的公钥 Ubuntu 的软件包管理工具(即dpkg和APT)要求软件包的发布者通过GPG密钥签名来确保 ...
- MySQL Base
/* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 ---> input pwd /* 数据库存贮引擎 */ InnoDB : 1) ...