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的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
随机推荐
- eclipse订制快捷键
步骤: 1.window-preference. 2.在(1)处输入keys,在(2)处输入命令的原来的快捷键,方便找到Binding,在(3)处输入自定义的快捷键.点击“apply and clos ...
- CodeForces - 762E:Radio stations (CDQ分治||排序二分)
In the lattice points of the coordinate line there are n radio stations, the i-th of which is descri ...
- exBSGS算法
BSGS,全称\(Baby Step Giant Step\),是用于求解离散对数的一种算法. 就是用来求\(A^x \equiv B (mod\ p)\) 的x这么一种算法-- 理论知识是:在[0, ...
- 解决js 运算 精度缺失
github地址: https://github.com/MikeMcl/big.js
- py-day1简单使用方法及语法使用详解
一.python入门 1.python的标准格式 创建python文件以.py结尾 如:vi hello.py #!/usr/bin/env python #-*- coding:utf-8 -*- ...
- 1.13-1.14 Hive Action
一.Hive Action 1.创建文件 [root@hadoop-senior oozie-apps]# pwd /opt/cdh-5.3.6/oozie-4.0.0-cdh5.3.6/oozie- ...
- 【网络爬虫】【python】网络爬虫(一):python爬虫概述
python爬虫的实现方式: 1.简单点的urllib2 + regex,足够了,可以实现最基本的网页下载功能.实现思路就是前面java版爬虫差不多,把网页拉回来,再正则regex解析信息--总结起来 ...
- 01 mybatis框架整体概况(2018.7.10)-
01 mybatis框架整体概况(2018.7.10)- F:\廖雪峰 JavaEE 企业级分布式高级架构师课程\廖雪峰JavaEE一期\第一课(2018.7.10) maven用的是3.39的版本 ...
- maven:mirrors和repository的关系区别
原文地址:http://my.oschina.NET/sunchp/blog/100634 1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相 ...
- .after()和.before()的关系
.after() 是在相邻元素后面插入元素 .next() 获得匹配元素集合中每个元素紧邻的同胞元素 用法介绍: $(selector).after(content) content 必需.规定要插入 ...