543 Diameter of Binary Tree 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。
示例 :
给定二叉树
1
/ \
2 3
/ \
4 5
返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。
注意:两结点之间的路径长度是以它们之间边的数目表示。
详见:https://leetcode.com/problems/diameter-of-binary-tree/description/
C++:
方法一:
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root)
{
int res = 0;
maxDepth(root, res);
return res;
}
int maxDepth(TreeNode* node, int& res)
{
if (!node)
{
return 0;
}
int left = maxDepth(node->left, res);
int right = maxDepth(node->right, res);
res = max(res, left + right);
return max(left, right) + 1;
}
};
方法二:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
int res=0;
maxDepth(root,res);
return res;
}
int maxDepth(TreeNode* node,int &res)
{
if(!node)
{
return 0;
}
if(m.count(node))
{
return m[node];
}
int left=maxDepth(node->left,res);
int right=maxDepth(node->right,res);
res=max(res,left+right);
return m[node]=(max(left,right)+1);
}
private:
unordered_map<TreeNode*,int> m;
};
参考:http://www.cnblogs.com/grandyang/p/6607318.html
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 二叉树的最大直径
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...
- [LeetCode] 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 ...
- Leetcode543.Diameter of Binary Tree二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它 ...
- 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的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
随机推荐
- 从数据的角度带你深入了解IPFS
IPFS 和区块链有着非常紧密的联系, 随着区块链的不断发展,对数据的存储需求也越来越高.本文从IPFS 的底层设计出发, 结合源代码, 分析了IPFS 的一些技术细节. 一.概述 IPFS 和区块链 ...
- JSTL取整、读取数组、字符串连接
以通过formatNumber去掉小数. <fmt:formatNumber type='number' value='${(tv.timeLong-tv.timeLong%60)/60 }' ...
- CISCO-从TFTP上上传/下载配置文件
1.下载配置文件到TFTP服务器: 2.上传配置文件到路由器
- HihoCoder 1502 : 最大子矩阵 (双指针)
描述 给定一个NxM的矩阵A和一个整数K,小Hi希望你能求出其中最大(元素数目最多)的子矩阵,并且该子矩阵中所有元素的和不超过K. 输入 第一行包含三个整数N.M和K. 以下N行每行包含M个整数,表示 ...
- 初始化cache_dir(squid)
sed -i '/adjustFactor/d' /CNCLog/exactTraffic/conf/localTraffic.cfgecho "adjustFactor = '-0.67 ...
- python程序的pypy加速
我们知道,python作为一种几乎是脚本语言的语言,其优点固然有,但是其有一个最大的缺点,就是运行速度没有办法和c,c++,java比.最近在些一些代码的时候也是碰到了这样的问题. 具体而言,pyth ...
- starUML安装与破解
安装包百度云: 链接:https://pan.baidu.com/s/1oF_DH7Xh6yun6fFUDB2H3w 密码:1z7e 破解步骤:1. 首先打开你的starUML安装目录,并找到Lice ...
- 洛谷 P3803 多项式乘法(FFT) —— FFT
题目:https://www.luogu.org/problemnew/show/P3803 终于学了FFT了! 参考博客:https://www.cnblogs.com/zwfymqz/p/8244 ...
- git远程提交到github或者gitee
一.git安装 git官网下载 https://git-scm.com/downloads ,既有Windows的.Linux的以及Macos的. windowds下载安装git的可执行文件,就会有两 ...
- mysql server安装(windows)
1 在 https://dev.mysql.com/downloads/mysql/ 上下载mysql压缩包 2 解压,并把bin目录加入环境变量 3 初始化,完成后会在mysql根目录下生成data ...