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 ...
随机推荐
- 今天升级Xcode 7.0 bata发现网络访问失败。
今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Securit ...
- C语言 printf格式化输出,参数详解
有关输出对齐 int main(int argc, char* argv[]){ char insertTime[20] = {"1234567890"}; double in ...
- 'dict_values' object does not support indexing, Python字典dict中由value查key
Python字典dict中由value查key 众所周知,字典dict最大的好处就是查找或插入的速度极快,并且不想列表list一样,随着key的增加越来越复杂.但是dict需要占用较大的内存空间,换句 ...
- JVM Object Query Language (OQL) 查询语言
Object Query Language (OQL) OQL is SQL-like query language to query Java heap. OQL allows to filter/ ...
- phpmyadmin后台4种拿shell方法 && php爆路径大法
php后台拿shell要知道php的路径,文章下面将讲诉爆php路径的方法!!! 方法一: CREATE TABLE `mysql`.`xss` (`xss1` TEXT NOT NULL ); IN ...
- 增强学习--Q-leraning
Q-learning 实例代码 import numpy as np import random from environment import Env from collections import ...
- java 把json对象中转成map键值对
相关:Json对象与Json字符串的转化.JSON字符串与Java对象的转换 本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据 比如json数据如下: {responseHeade ...
- 基于Spark Mllib,SparkSQL的电影推荐系统
本文测试的Spark版本是1.3.1 本文将在Spark集群上搭建一个简单的小型的电影推荐系统,以为之后的完整项目做铺垫和知识积累 整个系统的工作流程描述如下: 1.某电影网站拥有可观的电影资源和用户 ...
- jQuery UI加入效果
1.设计源代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- $ 和getElementId的区别 / 一个jquery对象的原型
请说出 div 和 $div 的联系和区别 区别 div 返回一个HTML DOM Object $div 返回一个 jQuery Object, 两者不等价 $div是包装了dom对象后产生的,无法 ...