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 ...
随机推荐
- UILabel设置行间距
UILabel设置行间距: NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWi ...
- ios 多线程之NSThread篇举例详解
这篇博客是接着总篇iOS GCD NSOperation NSThread等多线程各种举例详解写的一个支篇.总篇也包含了此文的链接.本文讲解的知识点有NSThread的开始.取消.在当前线程执行任务. ...
- Ubuntu 16.04下将ISO镜像制作成U盘启动的工具-UNetbootin(UltraISO的替代工具)
说明: 1.在Windows下制作ISO镜像的U盘启动工具有很多,但是在Linux平台下估计就只有UNetbootin这个工具最好用了,效果和Windows下的制作方法差不多,但是这个工具只能针对Li ...
- Interaction triggers in WPF
Interaction Class - static class that owns the Triggers and Behaviors attached properties. Handles p ...
- ThinkPHP模板中JS等带花括号处会被解析错误的解决办法
如下图,当本人在ThinkPHP框架的模板中写jQuery代码的时候,写了一些注释,并且注重是斜线和换括号{是连着一起的,这层语法上来时是没问题的,但是在ThinkPHP 的模板引擎解析下,会被解析掉 ...
- display:flex;多行多列布局学习
从以前的table布局到现在的div布局,再到未来的flex布局,CSS重构方面对展示行和适应性的要求越来越高: 首先来比较一下布局方式的更新意义: table布局: 优点:1.兼容性好,ie6.ie ...
- ActiveMQ安装与持久化消息
activityMQ官网:http://activemq.apache.org/ 有windows版与linux版 windows版启动 在bin目录下双击activemq.bat linux版的安 ...
- 百度display name为中文导致奔溃,productName和budlename区分出来
把"xxx-info.plist"中的"Bundle display name"的值改成了英文,或者把它的值修改成系统默认的"${PRODUCT_NA ...
- linux:使用comm命令比较两个文件:交集、差
linux:使用comm命令比较两个文件:交集.差 comm命令可以按行比较两个排序好的文件,输出有3列:第一列是file1独有的.第二列是file2独有的,第三列是两者都有的,简单语法如下:NAME ...
- 函数指针&指针函数
https://blog.csdn.net/luoyayun361/article/details/80428882