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 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 二叉树的最大直径的更多相关文章
- [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 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ...
- [leetcode]543. Diameter of Binary Tree二叉树的直径
题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 ...
- 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 ...
- 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 ...
随机推荐
- 【转】Eclipse的启动问题【an error has occurred see the log file】
原文网址:http://coderlin.blog.51cto.com/7386328/1275215 方法1: 今天打开Eclipse的时候出现来了一个问题,导致了Eclipse打不开 错误的提示是 ...
- Spring Boot 入门之缓存和 NoSQL 篇(四)
原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...
- winform Textbox模糊搜索实现下拉显示+提示文字
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 各大互联网公司2014前端笔试面试题–HTML,CSS篇
Html篇: 1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? IE: trident内核 Firefox:gecko内核 Safari:webkit内核 Opera:以前是presto ...
- javascript系列学习----Creating objects
在javascript语言里面,一切皆是对象,对象是它的灵魂,值得我们学习和领悟对象的概念和使用,下面我会引用实例来进行说明. 1)创建对象 方法一:js的对象方法构造 var cody = new ...
- Java-Runoob:Java Stream、File、IO
ylbtech-Java-Runoob:Java Stream.File.IO 1.返回顶部 1. Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出 ...
- Java-API-POI:POI 笔记
ylbtech-Java-API-POI:POI 笔记 1. 笔记一返回顶部 1. 1,POI对JDK版本支持及XLSX (2017-04-01 13:51:39)对JDK6的支持,最后版本是POI- ...
- 给scrapy添加代理IP
request.meta['proxy'] = 'http://'+'175.42.123.111:33995'
- [转]SQLite 经验集
SQLite 的默认时间 转自:http://www.cnblogs.com/pennant/archive/2011/08/11/2134897.html select strftime('%Y-% ...
- laravel5中添加自定义函数
laravel里面我们很多朋友不知道把自定义函数放在哪儿.我们的应用里经常会有一些全局都可能会用的函数,我们应该怎么放置它会比较好呢?现在匀们为大家准备了laravel放置函数的规范. 1. 创建文件 ...