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

Return 3, 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的更多相关文章

  1. Leetcode543.Diameter of Binary Tree二叉树的直径

    给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2    3 / \ 4  5 返回 3, 它 ...

  2. 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 ...

  3. LeetCode——Diameter of Binary Tree

    LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...

  4. 【leetcode_easy】543. Diameter of Binary Tree

    problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...

  5. [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 ...

  6. 543. Diameter of Binary Tree

    https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Mybatis更新用户

    xml配置 <!--更新用户 --> <update id="updateUserById" parameterType="com.itheima.my ...

  2. python的编码程序例题

    有一段python的编码程序如下:urllib.quote(line.decode("gbk").encode("utf-16")),请问经过该编码的字符串的解 ...

  3. Flex State

    在Flex 程序中,引入了状态设计的概念.在一个程序中,按照功能的需求,将界面切分成相对独立的部分.运行过程中,随着用户交互,界面在各个部分之间切换.比如在购物车程序中,登录界面.选购商品界面.购物车 ...

  4. 64位下安装Scrapy 报错 "could not find openssl.exe" 的解决方法。

    其实就是安装对应的64位 pyOpenSSL 就行了, 下载地址如下: https://tahoe-lafs.org/source/tahoe-lafs/deps/tahoe-lafs-dep-egg ...

  5. java实现发送邮件功能

    项目中实现发送邮件功能,先书写一个小Demo,记录如下: POM.XML中导入依赖 <!-- start java 提供的支持邮件发送相关业务的类 --> <dependency&g ...

  6. 后台SQL注入实例

    简要描述: 汉庭连锁酒店后台SQL注入,可绕过登陆限制进入后台,可脱库. 详细说明: 问题发生在这个站点.http://miaosha.htinns.com/ 标题内没有写具体信息.因为怕发布后被人入 ...

  7. 一起來玩鳥 Starling Framework(5)Multi-Touch

    這篇來談談Starling的Multi-Touch.前一篇也提到,Multi-Touch一樣是監聽TouchEvent.TOUCH,然後由TouchEvent的e.getTouches()取回多點的資 ...

  8. Java笔记5:单例模式

    一.应用杨景 在计算机系统中,线程池.缓存.日志对象.对话框.打印机.显卡的驱动程序对象常被设计成单例.这些应用都或多或少具有资源管理器的功能.每台计算机可以有若干个打印机,但只能有一个Printer ...

  9. Golang 内存热力图

    https://cizixs.com/2017/09/11/profiling-golang-program/

  10. Docker与PAAS

    Docker与PAAS 学习了:https://blog.csdn.net/raindaywhu/article/details/52057103 Docker基于内存的: