题目中的直径定义为:

任意两个节点的最远距离

没想出来,看的答案

思路是: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. Java读书计划和分享

    写在前面 为什么要写这些呢? 接触java已经有三年多了,感触颇多,比如从0到60,只要勤实践.勤思考,很快就可以入门,从60分到满分极致,则单单不是凭借工作年限或者什么就可以.曾经也有过一段迷茫时期 ...

  2. moviepy音视频剪辑:使用VideoFileClip、AudioFileClip和write_videofile、write_audiofile进行音视频的加载和输出

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 在本地进行音视频处理时,首先要从视频文件 ...

  3. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中当前列currentColumn和选中项selectedItems访问方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 当前列访问方法 树型部件QTreeWidget的currentColumn()方法返回当前项中得到焦 ...

  4. PyQt(Python+Qt)学习随笔:QListWidget的addItem方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在QListWidget对象中,增加一个项的方法是调用addItem方法,addItem方法有2种重 ...

  5. PyQt学习随笔:Qt事件QEvent.type类型常量及其含义资料汇总详细内容速查

    下表是Qt5.11提供的所有已经定义的事件类型常量及其含义说明(其中标蓝色的是老猿认为价值比较大的事件),事件的事件类型通过QEvent.type()来获取.由于老猿没有找到直接粘贴Excel表格的方 ...

  6. 【.Net Core】 使用 Nginx 发布 .Net Core 3.1 项目至LInux(Centos7)。

    前置博客(博客中使用的项目来自于此): [Docker] .Net Core 3.1 webapi 集成EF Code First,使用MySql进行业务操作 .配置swagger (三) 环境:.N ...

  7. java中的反射(一)

    在之前学java基础时学习过反射,但在因为没有相应的应用场景,所以也是一知半解.最近在学spring的时候发现反射是其中很重要的一部分,所以回顾一下,并看看它在spring中的应用. 目录 一.反射 ...

  8. CF392B Tower of Hanoi

    题目链接. Description 三塔汉诺塔问题,给一个 \(3 \times 3\) 的矩阵 \(t\),\(t_{i, j}\) 表示从 \(i\) 塔移动一个盘子到 \(j\) 塔的花费. 初 ...

  9. LibreOj #539. 「LibreOJ NOIP Round #1」旅游路线

    题目链接 做完这道题,我深知当一个问题复杂度过高的时候,把一些可以分离的操作都分散开,可以大幅度降低复杂度..... 发现无论有多少钱,每到一个点后扩展到的距离被限制在 \(min(C, c[i])\ ...

  10. 笨方法学python笔记

    编程是什么 编程就是通过输出一种语言给计算机"听",命令其去执行相应的操作. 我们称我们给计算机下达的命令称为指令.一般说程序就是有多个指令构成的. 计算机需要使用非常多的电路来实 ...