[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为helper的参数是左右两个点。结果是求深度的helper函数参数只有一个点:表示求一个点的最大深度,从简单做起。

[一句话思路]:

求一个点的最大深度,从简单做起。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

两条线段拼接时深度不用加一,单点的深度要加一。稍微注意下

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

两条线段拼接时深度不用加一,单点的深度要加一。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

求深度的参数只有一个点:

//define left, right
int left = depth(root.left);
int right = depth(root.right);

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0; public int diameterOfBinaryTree(TreeNode root) {
//corner case
if (root == null) {
return 0;
}
//depth
depth(root);
//return max;
return max;
} public int depth(TreeNode root) {
//corner case
if (root == null) {
return 0;
}
//define left, right
int left = depth(root.left);
int right = depth(root.right);
//renew max, don't add 1 since it's a sum of two lines
max = Math.max(max, left + right);
//return val for root
return Math.max(left, right) + 1;
}
}

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二叉树直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

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

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

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

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

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

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

  7. 【leetcode_easy】543. Diameter of Binary Tree

    problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...

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

  9. 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 b ...

随机推荐

  1. Spark Streaming性能调优

    数据接收并行度调优(一) 通过网络接收数据时(比如Kafka.Flume),会将数据反序列化,并存储在Spark的内存中.如果数据接收称为系统的瓶颈,那么可以考虑并行化数据接收.每一个输入DStrea ...

  2. 贴几个erlang文档站点

    国外三方的文档,比较全, http://erldocs.com/ 这个貌似是国内的版本,不是很新 http://erldoc.com/ 国内dhq大神的,也不是很新 http://dhq.me/erl ...

  3. VC编译选项 md /mdd /ml /mt/mtd

    VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd)C 运行时库                        库文件Single threa ...

  4. Range(转)

    原文链接:http://www.cnblogs.com/peida/p/Guava_Range.html 在Guava中新增了一个新的类型Range,从名字就可以了解到,这个是和区间有关的数据结构.从 ...

  5. 关于PIPELINE,也许你应该知道

    所谓流水线(pipeline)设计,通常来说是流水生产线. 同样,组合逻辑路径可以看作是一条生产线,路径上的每个逻辑单元都可以看作是一个阶段,都会产生时延. 如果不采用流水设计,前后级组合逻辑依次工作 ...

  6. OkHttp使用方法

    1.在app/build.gradle中添加依赖 compile 'com.squareup.okhttp3:okhttp:4.0.1' 2.创建OkHttpClient实例 OkHttpClient ...

  7. python 面向对象(其他相关)

    python 面向对象(其他相关): (思维导图    ↑↑↑↑↑↑) 一.issubclass(obj,cls) 检查obj是否是类cls的对象 class Base(object): pass c ...

  8. java代码---------打印正三角形

    总结:手打一遍 public class aaa{ public static void main(String []args){ for(int i=1;i<10;i++){ for(int ...

  9. 微服务监控之三:Prometheus + Grafana Spring Boot 应用可视化监控

    一.Springboot增加Prometheus 1.Spring Boot 应用暴露监控指标,添加如下依赖 <dependency> <groupId>org.springf ...

  10. mysql索引之一:索引基础(B-Tree索引、哈希索引、聚簇索引、全文(Full-text)索引区别)(唯一索引、最左前缀索引、前缀索引、多列索引)

    没有索引时mysql是如何查询到数据的 索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储10 ...