[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的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
随机推荐
- 转 - ubuntu 安装node.js 与 npm
原文链接为: https://blog.csdn.net/wangtaoking1/article/details/78005038 这篇文章介绍如何在ubuntu环境下安装node环境. 我使用的系 ...
- javascript创建对象之函数构造模式和原型模式结合使用(四)
创建自定义类型的常见方式就是组合使用构造函数模式与原型模式一起使用. 构造函数模式用于定义实例对象的特有的部分(属性和方法),原型模式用于定义共享的部分. 这样最大限度的节省了内存的开销. funct ...
- C++多线程同步之临界区(CriticalSection)
原文链接:http://blog.csdn.net/olansefengye1/article/details/53262917 一.Win32平台 1.相关头文件和接口 #include <w ...
- Spark分析之MemoryStore
private case class MemoryEntry(value: Any, size: Long, deserialized: Boolean) class MemoryStore(bloc ...
- 编码风格和PEP8规范
编码风格 错误认知 这很浪费时间 我是个艺术家 所有人都能穿的鞋不会合任何人的脚 我善长制定编码规范 正确认知 促进团队合作 减少bug处理 提高可读性,降低维护成本 有助于代码审查 养成习惯,有助于 ...
- leetcode754
class Solution { public: int reachNumber(int target) { // 理解这题的意思 这题就好做了 // 分析 首先考虑一种比较极端的情况 即一直向正方向 ...
- VBA 禁止保存
禁止保存 在workbook事件中 Private Sub Workbook_BeforeClose(Cancel As Boolean) Me.Saved = TrueEnd Sub Priv ...
- UI5-文档-4.26-(Optional) Remote OData Service
到目前为止,我们已经使用了本地JSON数据,但是现在我们将访问一个真正的OData服务来可视化远程数据. 用可公开获得的Northwind OData服务显示并替换发票模型的JSONModel类型,以 ...
- UI5-文档-4.4-XML Views
将所有UI放到index.html文件将很快导致一个混乱的设置,有相当多的工作在我们前面.我们先用sap.m.Text进行模块化.控件导入专用视图. SAPUI5支持多种视图类型(XML.HTML.J ...
- heat 用法 示例
heat.exe dir "./SampleFolder" -cg Files -dr INSTALLDIR -gg -g1 -sf -srd -var "var.Tar ...