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

  1. class Solution {
  2. public:
  3. int depth(TreeNode* root)
  4. {
  5. if (root == NULL) return 0;
  6. return max(depth(root->left),depth(root->right))+1;
  7. }
  8. int depthDiff(TreeNode* root)
  9. {
  10. if(root == NULL) return 0;
  11. return depth(root->left)+depth(root->right);
  12. }
  13. int diameterOfBinaryTree(TreeNode* root) {
  14. if(root == NULL) return 0;
  15. return max(depthDiff(root),max(diameterOfBinaryTree(root->left),diameterOfBinaryTree(root->right)));
  16. }
  17. };

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

  1. class Solution {
  2. public:
  3. int maxdiadepth = 0;
  4. int dfs(TreeNode* root){
  5. if(root == NULL) return 0;
  6. int leftdepth = dfs(root->left);
  7. int rightdepth = dfs(root->right);
  8. if(leftdepth + rightdepth > maxdiadepth) maxdiadepth = leftdepth + rightdepth;
  9. return max(leftdepth +1, rightdepth + 1);
  10. }
  11. int diameterOfBinaryTree(TreeNode* root) {
  12. dfs(root);
  13. return maxdiadepth;
  14. }
  15. };

C++_Recursive_with brief explanation

  1. class Solution {
  2. public:
  3. int diameterOfBinaryTree(TreeNode* root) {
  4. if(root == nullptr) return 0;
  5. int res = depth(root->left) + depth(root->right);
  6. return max(res, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right)));
  7. }
  8. int depth(TreeNode* root){
  9. if(root == nullptr) return 0;
  10. return 1 + max(depth(root->left), depth(root->right));
  11. }
  12. };

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. golang的reflect

    引用自 http://www.jb51.net/article/115002.htm 和 C 数据结构一样,Go 对象头部并没有类型指针,通过其自身是无法在运行期获知任何类型相关信息的.反射操作所需要 ...

  2. 【spring data jpa】使用jpa进行update更新操作,避免空字段和更新后即时返回给前台的问题

    修改操作  如果使用的JPA,应该是 findByUid之后 将新的值对old进行赋值,最后对old值进行flush(),即可完成更新,而不应该去更新新的实体. 示例代码: 以后补充

  3. Devexpress汉化修改 已经汉化过后生成的*.resources.dll文件 z

    Devexpress 是很好的第三方控件.但是需要汉化,在从网上下载的zh-CN文件夹里都是*.resources.dll文件,由于汉化的工作量很大,难免有些地方汉化 错误或者不合适(如下图).花了些 ...

  4. CSS3:animation动画

    animation只应用在页面上已存在的DOM元素上,学这个不得不学keyframes,我们把他叫做“关键帧”. keyframes的语法法则: @keyframes flash { from{ le ...

  5. SpringMVC学习记录(七)--拦截器的使用

    SpringMVC的请求如以下这样的图所看到的: 能够看出全部的请求都要通过Dispatherservlet来接收,然后通过Handlermapping来决定使用哪个控制器,再依据ViewResolv ...

  6. Actionscript 3 自定义 matedata

    metadata就是元数据 反应一个类本质的属性 可以通过describeType(obj)来得到反应该对象的xml 要自定义元数据,如[MyMatedata()] package {  public ...

  7. Kubernetes用户指南(三)--在生产环境中使用Pod来工作、管理部署

    一.在生产环境中使用Pod来工作 本节将介绍一些在生产环境中运行应用非常有用的功能. 1.持久化存储 容器的文件系统只有当容器正常运行时有效,一旦容器奔溃或者重启,所有对文件系统的修改将会丢失,从一个 ...

  8. 通过终端安装程序sudo apt-get install ***时出错:

    E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)  E: Unable to ...

  9. 个人或者企业怎么进行app开发?开发一款APP应用大概须要多少钱?

    App开发.是指专注于手机应用软件开发与服务. App是application的缩写,通常专指手机上的应用软件,或称手机client.另外眼下有非常多在线app开发平台.当然移动互联网时代是全民的移动 ...

  10. iOS学习笔记23-音效与音乐

    一.音频 在iOS中,音频播放从形式上能够分为音效播放和音乐播放. * 音效: * 主要指一些短音频的播放,这类音频一般不须要进行进度.循环等控制. * 在iOS中.音效我们是使用AudioToolb ...