【leetcode_easy】543. Diameter of Binary Tree
problem
题意:
转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢。那么我们只要对每一个结点求出其左右子树深度之和,这个值作为一个候选值,然后再对左右子结点分别调用求直径对递归函数,这三个值相互比较,取最大的值更新结果res,因为直径不一定会经过根结点,所以才要对左右子结点再分别算一次。为了减少重复计算,我们用哈希表建立每个结点和其深度之间的映射,这样某个结点的深度之前计算过了,就不用再次计算了。
solution1: 两个递归函数。
/**
* 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) {
if(root==NULL) return ;
int ans = getHeight(root->left) + getHeight(root->right);
return max(ans, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right)));
}
int getHeight(TreeNode* node)
{
if(node==NULL) return ;
if(mymap.count(node)) return mymap[node];
int h = + max(getHeight(node->left), getHeight(node->right));
mymap[node] = h;
return h;
}
private:
unordered_map<TreeNode*, int> mymap;
};
solution2: 一个递归函数。
只需要用一个递归函数就可以了,可以在求深度的递归函数中顺便就把直径算出来,而且貌似不用进行优化也能accept。
solution3:
优化版本。
参考
1. Leetcode_easy_543. Diameter of Binary Tree;
2. Grandyang;
完
【leetcode_easy】543. Diameter of Binary Tree的更多相关文章
- 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【leetcode】543. Diameter of Binary Tree
题目如下: 解题思路:最长的周长一定是树中某一个节点(不一定是根节点)的左右子树中的两个叶子节点之间的距离,所以最简单的办法就是把树中所有节点的左右子树中最大的两个叶子节点之间的距离求出来,最终得到最 ...
- 【Leetcode_easy】993. Cousins in Binary Tree
problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 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 ...
- 【CF438E】The Child and Binary Tree(多项式运算,生成函数)
[CF438E]The Child and Binary Tree(多项式运算,生成函数) 题面 有一个大小为\(n\)的集合\(S\) 问所有点权都在集合中,并且点权之和分别为\([0,m]\)的二 ...
- 543. Diameter of Binary Tree【Easy】【二叉树的直径】
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- 【Leetcode】【Easy】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 543. Diameter of Binary Tree
https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...
随机推荐
- 如何判断PHP空间是否支持curl、gzip等功能
在网站根目录新建v.php,输入以下代码: <?php $f=@trim($_GET['f']); if(function_exists($f)) echo '支持'.$f; else echo ...
- 【转】关于 Goroutine Channel Select 的用法和理解
原文:https://blog.csdn.net/jfkidear/article/details/88661693 ----------------------------------------- ...
- laravel本地开发环境的安装及配置 - Windows:安装 Laravel Homestead 虚拟机
一.安装 VirtualBox-5.2.22-126460-Win.exe 和 vagrant_2.2.2_x86_64.msi(可视化安装包安装); 安装在D盘 二.导入 Homestead Vag ...
- Disconnected from the target VM, address: '127.0.0.1:56577', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:56577', transport: 'socket' Disconnected from t ...
- jQuery遍历not的用法
从包含所有段落的集合中删除 id 为 "selected" 的段落: $("p").not("#selected") 定义和用法 not() ...
- list交集
在C#语言程序设计中,List集合是常用的集合数据类型,在涉及集合类型的运算中,有时候我们需要计算2个List集合中共有的数据,即对2个List集合求交集运算.此时可以使用C#语言提供的Interse ...
- mybatis+mysql批量插入和批量更新
一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo,oo,oo) mybatis中mapper.x ...
- java web项目中后台控制层对参数进行自定义验证 类 Pattern
Pattern pattern = Pattern.compile("/^([1-9]\d+元*|[0]{0,1})$/");//将给定的正则表达式编译到模式中 if(!" ...
- 注册emf package并读取EMF文件
/** * 读EMF文件 * * @param uri * @return */ public static Resource readEMFFile(URI uri) { ResourceSet r ...
- dp四边形优化
如果dp[i][j]=min(dp[i][k]+dp[k+1][j]+w[i][j]);且满足dp[a][c]+dp[b][d]<=dp[a][d]+dp[c][d](a<b<=c& ...