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 ...
随机推荐
- #231-D: declaration is not visible outside of function
通常出现在.h中的函数声明,函数声明中的变量用到了.h当中没有的变量形式,比如特殊定义的结构体
- wordpress分享到微信无缩略图的问题
牛逼的老板想把他牛逼的艺术品分享给牛逼的画家和藏家简直苦逼了我这个程序狗,他想要的结果是这样的qq和微信上依次为 但是牛逼的微信越来越“抠”了现在不是微信开发者用不了他的分享接口不给你显示缩略 ...
- 关于ng-class,ng-style的用法
ng-class的使用几种方式 (1):利用双向数据绑定(className根据chang2的值去匹配类) <div class="{{className}}">... ...
- windows下使用linux命令搜文件
在linux系统的黑幕下,我们只能通过find命令来搜文件,而在windows的界面里,我们只能通过搜索框来搜.两者各有利弊,前者单调但高效,后者用户体验好却比较慢.能不能在windows使用linu ...
- ASP.NET Ajax 客户端框架未能加载、"Sys"未定义
在Windows Server 2003 系统上部署asp.net项目出现以下问题: IIS为6.0 导致菜单控件的图片显示不出来: WebSite:程序中的图片显示正常. 但用到ASP.net 2. ...
- docker怎么破?
为什么要装docker? 因为linux服务器不好用,很多操作不好进行,比如安装包没有管理员权限 docker可以访问本地显卡,比一般的virtual box 或者VMware都要好 怎么装docke ...
- h5 禁止微信内置浏览器调整字体大小方法
ios 通过重写样式控制 body { -webkit-text-size-adjust:100%!important; } android 通过重写事件控制 (function() { if (ty ...
- Vmware虚拟机linux上网问题
1.虚拟机linux上网问题 1.1 VMware中虚拟机网络的三种设置 第一种:桥接(bridged) 第二种:NAT 第三种:Host only . 这种模式下仅主机可以上网,虚拟机不能上网. 1 ...
- nginx与tomcat整合
nginx与tomcat整合 1. 在/usr/local/nginx/conf下面添加文件proxy.conf # cat /usr/local/nginx/confg/proxy.conf p ...
- angularJS开发时用到的命令
node --version && npm --version 查看nodejs版本号和npm版本号 yo --version && bower --version & ...