题目中的直径定义为:

任意两个节点的最远距离

没想出来,看的答案

思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1))

遍历并更新结果

int res = 1;
public int diameterOfBinaryTree(TreeNode root) {
helper(root);
return res-1;
}
public int[] helper(TreeNode root)
{
//两个量分别是节点深度,节点最大diameter
if (root==null) return new int[]{0,0};
int cur = 0;
int[] l = helper(root.left);
int[] r = helper(root.right);
cur = Math.max(Math.max(l[1],r[1]),l[0]+r[0]+1);
res = Math.max(res,cur);
return new int[]{Math.max(l[0],r[0])+1,cur};
}

[leetcode]543. Diameter of Binary Tree二叉树的直径的更多相关文章

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

  2. LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)

    题目: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of ...

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

  4. 543 Diameter of Binary Tree 二叉树的直径

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

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

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

  7. 543. Diameter of Binary Tree 二叉树的最大直径

    [抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...

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

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

  9. [leetcode] 543. Diameter of Binary Tree (easy)

    原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...

随机推荐

  1. 图像分割必备知识点 | Unet++超详解+注解

    文章来自周纵苇大佬的知乎,是Unet++模型的一作大佬,其在2019年底详细剖析了Unet++模型,讲解的非常好.所以在此做一个搬运+个人的理解. 文中加粗部分为个人做的注解.需要讨论交流的朋友可以加 ...

  2. 2017 Mid Central Regional G.Hopscotch (组合计数)

    这道题有点意思,给出点(N,N),你在原点处向目标点走,每次只能向x和y两个方向走路,每次xy两个方向的步幅分别不能小于dx和dy,问走到终点的方案数,答案对1e9 + 7取模 这道题最直接的想法就是 ...

  3. AlanShan数据库课程设计报告

    目    录 1.绪论.... 2 1.1前言... 2 1.2社会背景... 2 1.3超市背景... 3 2.系统可行性研究.... 4 2.1 技术可行性研究... 4 2.2 经济可行性研究. ...

  4. springboot多模块项目搭建遇到的问题记录

    废话不多说,直接上问题报错与解决方法. 问题报错一:(报错信息看下方代码) 问题原因:'com.company.logistics.service.company.CompanyService' 未找 ...

  5. scrapy爬虫爬取小姐姐图片(不羞涩)

    这个爬虫主要学习scrapy的item Pipeline 是时候搬出这张图了: 当我们要使用item Pipeline的时候,要现在settings里面取消这几行的注释 我们可以自定义Item Pip ...

  6. [BJDCTF 2nd]简单注入

    [BJDCTF 2nd]简单注入 hint.txt出现了内容. 大概意思和国赛一道题相同. username处注入\来转义单引号,password处使用sql语句整数型注入. 例如: 传入admin\ ...

  7. Error.name 六种值对应的信息

    1 EvalErroe:eval()  的使用与定义不一致 2 RangrError: 数值越界 3 ReferenceError:非法或不能识别的引用数值 4 SyntaxError:发生语法解析错 ...

  8. Python-Wechaty: 面向所有IM软件的聊天机器人框架

    Author: wj-Mcat Code: python-wechaty 个人开发项目,且行且不易,有感兴趣的朋友可以去给一波关注,你们的支持就是我最大的动力,谢谢大家. Python-wechaty ...

  9. 团队作业6——Alpha阶段项目复审

    复审人:利国铭 复审人看什么: 软件的质量:解决原计划解决的问题了么,软件运行质量如何?用户有多少,用户反馈如何? 软件工程的质量:代码在哪里? 代码能在新的机器上构建成功么? 代码可维护性如何?每日 ...

  10. react项目引入使用element-react报错

    解决办法 npm i react-hot-loader@next -D