LeetCode543. Diameter of Binary Tree
Description
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
Return3, which is the length of the path[4,2,1,3]or[5,2,1,3].
my program
思路:
int depth(TreeNode* root)求树的高度,int depthDiff(TreeNode* root)求root的左子树和右子树的高度和。递归求树的diameter
class Solution {
public:
int depth(TreeNode* root)
{
if (root == NULL) return 0;
return max(depth(root->left),depth(root->right))+1;
}
int depthDiff(TreeNode* root)
{
if(root == NULL) return 0;
return depth(root->left)+depth(root->right);
}
int diameterOfBinaryTree(TreeNode* root) {
if(root == NULL) return 0;
return max(depthDiff(root),max(diameterOfBinaryTree(root->left),diameterOfBinaryTree(root->right)));
}
};
Submission Details
106 / 106 test cases passed.
Status: Accepted
Runtime: 19 ms
Your runtime beats 18.82 % of cpp submissions.
other methods
C++ Solution with DFS
class Solution {
public:
int maxdiadepth = 0;
int dfs(TreeNode* root){
if(root == NULL) return 0;
int leftdepth = dfs(root->left);
int rightdepth = dfs(root->right);
if(leftdepth + rightdepth > maxdiadepth) maxdiadepth = leftdepth + rightdepth;
return max(leftdepth +1, rightdepth + 1);
}
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return maxdiadepth;
}
};
C++_Recursive_with brief explanation
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
if(root == nullptr) return 0;
int res = depth(root->left) + depth(root->right);
return max(res, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right)));
}
int depth(TreeNode* root){
if(root == nullptr) return 0;
return 1 + max(depth(root->left), depth(root->right));
}
};
LeetCode543. Diameter of Binary Tree的更多相关文章
- 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——Diameter of Binary Tree
LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...
- 【leetcode_easy】543. Diameter of Binary Tree
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
- [Swift]LeetCode543. 二叉树的直径 | 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
https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...
- 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] 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&Python] Problem 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 ...
随机推荐
- kill -3 获取threaddump信息
有些Java应用服务器是在控制台上运行,如Weblogic,为了方便获取threaddump信息,在weblogic启动的时候,会将其标准输出重 定向到一个文件,用"nohup ./star ...
- Linux下的/etc/crontab文件和crontab -e命令区别及Crontab命令详解(转)
/etc/crontab文件和crontab -e命令区别 1.格式不同 前者 # For details see crontabs # Example of job definition: # .- ...
- 大规模请求下,Linux 服务器连接数优化设置
作者:heiyeluren 一般一个大规模Linux服务器请求数可能是几十万上百万的情况,需要足够的连接数来使用,所以务必进行相应的设置. 默认的Linux服务器文件描述符等打开最大是1024,用 u ...
- [Javascript]js判断是否为undefined类型
概述 在项目获取某个元素的值会出现undefined,所以对这种情况要有特殊处理. 可通过下面的代码判断是否为undefined类型. if (typeof(reValue) == "und ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.1.安装并配置ASM驱动
3.1.安装并配置ASM驱动 3.3.1.检查内核 [root@linuxrac2 etc]# uname -r 2.6.18-164.el5 下载以下rpm包(注意rpm包版本和Linux内核版本一 ...
- Less使用说明
使用koala编译 Koala 是一款由国人开发的开源预处理语言图形编译工具,目前已支持 Less.Sass.Compass 与CoffeeScript. 目前支持以下系统:Windows,Mac, ...
- oracle 10g函数大全--其他函数
DUMP(w[,x[,y[,z]]]) [功能]返回数据类型.字节长度和在内部的存储位置. [参数] w为各种类型的字符串(如字符型.数值型.日期型……) x为返回位置用什么方式表达,可为:8,10, ...
- Windows如何使用主题包
相信有很多朋友都遇到这样的问题 下载了主题却没有办法安装就是解压到C:\WINDOWS\Resources\Themes也是于事无补 没有任何反应 回到桌面 点击右键还是没有没有任何反应见图一 原因很 ...
- Python——调用shell命令的三种方法
1.用os.system(cmd) 不过取不了返回值 2.用os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd ...
- eclipse工程重命名后,无法生产class问题
在很多时候我们对project重新命名后,class文件始终没有生产,尤其是在web项目的时候,如果不注意class文件生成问题,会浪费大量的时间找错误.这里分享下如何解决eclipse重命名后cla ...