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的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
随机推荐
- Appium——连接真机,adb devices获取不到设备号
连接真机后,使用 adb devices获取不到设备号. 1.检查usb接口是否正常,是否正常链接到电脑 2.手机开发者模式是否开启,usb调试是否开启 3.检查驱动是否正常 4.如果驱动显示黄叹号, ...
- html5--3.16 button元素
html5--3.16 button元素 学习要点 掌握button元素的使用 button元素 用来建立一个按钮从功能上来说,与input元素建立的按钮相同 button元素是双标签,其内部可以配置 ...
- 【转载】String和StringBuffer的区别,以及StringBuffer的常用方法介绍
String与StringBuffer的区别简单地说,就是一个变量和常量的关系.StringBuffer对象的内容可以修改:而String对象一旦产生后就不可以被修改,重新赋值其实是两个对象.Stri ...
- input处理函数
input处理函数是潜在的影响你app性能的问题,他们可以阻止帧的形成,并且可以造成多余的亦或不必要的layout的工作. 避免长时间运行input handler:它们会阻塞scroll 不要在in ...
- java+poi实现word转html显示
直入正题,需求为页面预览word文档,用的是poi3.8,以下代码支持表格.图片,不支持分页,只支持doc,不支持docx: 1.导jar包 2.java文件 /** * */ import java ...
- MSTAR SETBOX 常用API
HASHKEY: HashKey需要替换两个文件:“Customer_info.h”与“libecos.a” 1.MApi_DigiTuner_TPSGetLock() //判断是否有信号 2._Za ...
- 普通用户Mysql 5.6.13 主从,主主及nagios的mysql slave监控
Master:192.168.209.19 Slave:192.168.209.20 mysql版本:mysql5.6.13 1. 以root身份创建普通用户,如mysql,并创建mysql安装目录: ...
- 升级python之后yum不可用
#修改yum文件 vi /usr/bin/yum 将文件头部的 #!/usr/bin/python 改为默认的python文件,例如 #!/usr/bin/python2.7.3 整个过程完成了. 同 ...
- 自已封装Ajax方法
function createXHR() { var request; if (typeof (XMLHttpRequest) == 'undefined') { request = new Acti ...
- int和Integer及拆箱与装箱
int和Integer 如果面试官问Integer与int的区别:估计大多数人只会说道两点,Ingeter是int的包装类,int的初值为0,Ingeter的初值为null.但是如果面试官再问一下In ...