[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 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].
题目
给定一棵二叉树,求任意两个节点的最长路径长度。
思路
长度的定义是边的个数,不是node的个数
跟 [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和 思路一致。
代码
class Solution {
public int diameterOfBinaryTree(TreeNode root) {
/* 要么用个global variable放在class下,要么用长度为1的一维数组来存。
这里因为求edge的数量,初始化为一维数组的default值0是可行的。
*/
int[] diameter = new int[1];
dfs(root, diameter);
return diameter[0];
}
private int dfs(TreeNode node, int[] diameter) {
if(node == null){return 0;}
int lh = dfs(node.left, diameter);
int rh = dfs(node.right, diameter);
diameter[0] = Math.max(diameter[0], lh + rh);
return Math.max(lh, rh) + 1;
}
}
[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 ...
- 543. Diameter of Binary Tree 二叉树的最大直径
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...
- [leetcode] 543. Diameter of Binary Tree (easy)
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...
- 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的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
随机推荐
- PHPExcel导入导出 若在thinkPHP3.2中使用(无论实例还是静态调用(如new classname或classname::function)都必须加反斜杠,因3.2就命名空间,如/classname
php利用PHPExcel类导出导入Excel用法 来源: 时间:2013-09-05 19:26:56 阅读数: 分享到: 16 [导读] PHPExcel类是php一个excel表格处理插 ...
- TensorFlow相关的一些技巧
谷歌开发技术推广工程师 Laurence Moroney 在 Google Cloud Next 大会上进行了一段 42 分钟的演讲,主题是「What's New with TensorFlow?」. ...
- Xshell批量导入IP地址
我的xshell被覆盖了~~~结果原来的host没了,很郁闷要一个一个添加,网上找了很长时间在Xshell中批量添加IP的文章,结果都不行. 最后找到了https://blog.netsarang.c ...
- zabbix自动发现与监控内存和CPU使用率最高的进程,监测路由器
https://cloud.tencent.com/info/488cfc410f29d110c03bcf0faaac55b2.html (未测试) https://www.cnblo ...
- windows脚本测试
一. C:\Users\smc892h>systeminfo | findstr 物理内存物理内存总量: 12,167 MB可用的物理内存: 2,103 MB 二.截取字段 参考网站 ...
- HTML5 Canvas ( 文字横纵对齐 ) textAlign, textBaseLine
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 更新403 Forbidden
用TidHTTP下载一个文件 http://192.168.1.1/files/新建文本文档.txt 报错,但是用浏览器打开正常 HTTP/1.1 403 Forbidden 最后不是程序的问题,把i ...
- c++builder XE7 C++11 C++0x 新语法
Non-static data member initializers 非静态成员变量初始化变得简单,这个XE7 64位编译成功,32位还是不支持 As a simple example, struc ...
- 13 python 常用的内置方法介绍
1.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(object) ...
- UI5-文档-4.16-Dialogs and Fragments
在这一步中,我们将进一步研究另一个可以用来组装视图的元素:the fragment. 片段是轻量级UI部件(UI子树),可以重用,但是没有任何控制器.这意味着,每当你想定义一个特定UI的一部分是跨多个 ...