LeetCode算法题-Diameter of Binary Tree(Java实现)

离线安装samba与配置(.tar方式安装) 个人记录 阅读详情

这是悦乐书的第257次更新,第270篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第124题(顺位题号是543)。给定二叉树,您需要计算树的直径长度。 二叉树的直径是树中任意两个节点之间最长路径的长度。 此路径可能会也可能不会通过根节点。例:

给出一棵二叉树

    1
   / \
  2   3
 / \
4   5

返回3,这是路径[4,2,1,3]或[5,2,1,3]的长度。

注意:两个节点之间的路径长度由它们之间的边数表示。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 解题

题目要求是求二叉树的直径,根据题目给的示例,也可以理解成从根节点开始,左子树的深度与右子树的深度之和,但是题目又说了,也可能不经过根节点,所以我们需要求两者的最大值。计算任意一个节点的最长路径值,在其中取出最大值即可。递归方法的主体,和求二叉树最大深度的代码一样,只是中间加了一步取最大值的操作而已。

此解法的时间复杂度是O(n),空间复杂度是O(n)。

public int diameterOfBinaryTree(TreeNode root) {
    helper(root);
    return max;
}

private int max = 0;
public int helper(TreeNode root) {
    if (root == null) {
        return 0;
    }
    int left = helper(root.left);
    int right = helper(root.right);
    max = Math.max(max, left+right);
    return Math.max(left, right)+1;
}

03 小结

算法专题目前已日更超过三个月,算法题文章124+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

姿态传感器/陀螺仪之BMI270应用原理图设计 BMI270是一款由博世(Bosch)推出的高性能、超低功耗惯性测量单元(IMU),专为可穿戴设备、智能设备及物联网应用设计。 阅读详情

相关推荐

光耦继电器电路分析

光耦电路的分析笔记

MingYue_SSS的博客 6875

算法导论-22.2-7-树的直径

一、题目 树T=(V,E)的直径(diameter)定义为max(u,v),亦即,树的直径是树中所有最短路径长度中的最大值。试写出计算树的直径的有效算法,并分析算法的运行时间。 二、思考 step1:以树中任意一个结点为源点,进行一次广度优先遍历,找出离源点距离最远的点d step2:以d为源点,进行一次广度优先遍历,找出离d最远的点,并记录其长度 三、代码 //用邻接

幸福在路上 6505

足球大数据分析入门:从赔率到预测的完整流程(附数据集)

本文提供了一份足球大数据分析的完整实战指南,涵盖从数据采集、清洗、特征工程到模型构建与评估的全流程。文章详细介绍了如何利用公开数据集与API获取数据,并通过逻辑回归、随机森林等模型进行比赛结果预测,最后强调了模型评估与策略回测的重要性,帮助读者构建自己的分析框架。

r7s8t的博客 404

Diameter of Binary Tree

二叉树的直径,即二叉树中一个节点到另一个节点的最大路径 采用分治和递归的思想:根节点为root的二叉树的直径 = Max(左子树直径,右子树直径,左子树的最大深度(不包括根节点)+右子树的最大深度(不包括根节点)) private int diameter = 0; private int DiameterofBinaryTree(Algorithm.TreeNode he...

Wenbooboo的博客 359

AWR射频仿真基本操作(一)

这里只讲述一个基本的操作: 软件是AWR 14版本

钢丝 2万+

高斯过程的数学理解

G澳大利亚的过程是有益的,特别是当我们有少量数据时。当我在制造业担任数据科学家时,我们的团队使用这种算法来揭示我们接下来应该进行哪些实验条件。但是,此算法不如其他算法流行。在这篇博客中,我将通过可视化和 Python 实现来解释高斯过程 [1] 的数学背景。

gongdiwudu的专栏 3606

LeetCode 543. Diameter of Binary Tree——JAVA Faster than 100%

问题可以转换为节点左右子树的深度之和,但是左右子树深度和最大的节点不一定是根节点,所以需要在计算左右子树深度的递归过程中,需要用Math.max函数记录左右子树和最大的情况。 class Solution { int len; public int diameterOfBinaryTree(TreeNode root) { if(null==root) ...

Juliussss 206

9.1 机器人动力学建模

目录9.1 机器人动力学建模一、机器人动力学的基本概念1. 什么是机器人动力学?2. 动力学方程二、机器人动力学建模方法1. 拉格朗日方法2. 牛顿-欧拉方法三、Simulink 中的机器人动力学建模1. 使用 Simscape Multibody 建模示例:构建一个两连杆机械臂的动力学模型。步骤:2. 使用 Robotics System Toolbox 建模示例:构建一个六自由度工业机器人的动力学模型。步骤:四、示例:两连杆机械臂的动力学建模目标:步骤:五、结果分析1. 动态响应2. 参数敏感性六、总结

xiaoheshang_123的博客 626

leetcode解题之543. Diameter of Binary Tree Java版 (二叉树的最大直径)

leetcode解题之543. Diameter of Binary Tree Java版 (二叉树的最大直径)

mine_song的博客 3256

Python爬虫:抓取餐饮平台顾客评分与评价的完整教程

本文介绍了如何使用Python爬虫技术抓取餐饮平台上的餐厅评分和评论数据。我们通过三种主要的抓取方法:静态网页抓取(requests)、动态网页抓取(Selenium)和API抓取,展示了如何抓取餐饮平台的数据。还介绍了如何使用pandas对抓取的数据进行存储、清洗、分析,并进行可视化和情感分析。通过本文的学习,您应该能够掌握如何使用Python爬虫抓取餐饮平台的评论数据,并将这些数据应用于市场分析、顾客反馈和服务优化等领域。

2201_76125261的博客 1144

博途PLC模糊PID控制(量化因子和比例因子)

参数"量化因子和比例因子",都是为了对清晰值进行比例变换而设置的,其作用是使变量按一定比例进行放大或缩小,以便和我们的模糊论域相匹配完成一一映射。这种变换对整个系统的工作性能也是有影响的。(其实相当于额外引入了比例作用,所以说模糊PID的调节是个系统工程)。限于本人能力,文中难免出现错误和不足,欢迎大家批评和指正。博途PLC的模糊PID控制完整算法可以参看下面这篇文章博途PLC模糊PID控制详解(SCL完整源代码+参数)_RXXW_Dor的博客-优快云博客。......

RXXW_Dor的博客 5269

LeetCode-543. Diameter of Binary Tree [C++][Java]

Given therootof a binary tree, returnthe length of thediameterof the tree. Thediameterof a binary tree is thelengthof the longest path between any two nodes in a tree. This path may or may not pass through theroot.

贫道绝缘子的博客 653

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode | 0543. Diameter of Binary Tree二叉树的直径【Python】

LeetCode 0543. Diameter of Binary Tree二叉树的直径【Easy】【Python】【递归】 Problem LeetCode Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the ...

Wonz 607

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode-Diameter of Binary Tree

LeetCode Java Diameter of Binary Tree

坟头草一米七五 204

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

LeetCode 543. Diameter of Binary Tree

LeetCode 543. Diameter of Binary Tree (Easy) 主要知识点:树;优先级:4

温迪今天刷题了吗 365

【Leetcode】543. Diameter of Binary Tree

思路: 通过递归计算左右子树的深度来计算根节点的diameter(记为temp),通过和类变量 diameter 进行比较,保存较大值。 在每一次递归结束后,返回左右子树的深度,将二者相加再加2就是根节点的diameter。 /** * Definition for a binary tree node. * public class TreeNode { * int

筱葭的博客 2942

LeetCode 543. Diameter of Binary Tree【Java】

题目描述 二叉树的直径 AC代码 枚举经过每一个节点的路径最大值,根据这个值去更新全局的直径,最终得到的就是直径。左右两边的子树是独立的,那么想要获得当前节点的路径最大值,只需要求出来左边路径的最大值和右边路径的最大值,然后相加就可以了,其实就是求左右子树的深度之和。 注意:题目中也提到了,不一定要经过根节点。 /** * Definition for a binary tree node. ...

NayelyA的博客 280

LeetCode543. Diameter of Binary Tree

Leetcode543. Diameter of Binary Tree

正西风落叶下长安 770

[LeetCode]543. Diameter of Binary Tree

https://leetcode.com/problems/diameter-of-binary-tree/#/description 树的半径 根据这个定义可以直接转换出“树的半径“ == 最大左右子树深度和 public class Solution { int max = 0; // 这个所谓的半径就是左右子树深度和

gqk289的专栏 1978

Leetcode 543 Diameter of Binary Tree

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 binary tree is the length of the longest path between any two no...

weixin_43876597的博客 166

【LeetCode】 543. Diameter of Binary Tree 二叉树的直径(Easy)(JAVA)

【LeetCode】 543. Diameter of Binary Tree 二叉树的直径(Easy)(JAVA) 题目地址: https://leetcode.com/problems/diameter-of-binary-tree/ 题目描述: Given a binary tree, you need to compute the length of the diameter of the...

qq_16927853的博客 232

Leetcode 543. Diameter of Binary Tree(递归)

Leetcode 543. Diameter of Binary Tree 题目链接: [Diameter of Binary Tree](https://leetcode.com/problems/Diameter of Binary Tree/) 难度:Medium 题目大意: 详见题意。 思路: 利用递归求二叉树的最深深度,在这个过程中再求二叉树的直径。 代码 /** * Definition for a binary tree node. * public class TreeNode {

xue1236的博客 168

LeetCode - Easy - 543. Diameter of Binary Tree

Topic Tree Description https://leetcode.com/problems/diameter-of-binary-tree/ Given the root of a binary tree, return 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.

KISS 234

LeetCode Top 100 Liked Questions 543. Diameter of Binary Tree (Java版; Easy)

welcome to my blog LeetCode Top 100 Liked Questions 543. Diameter of Binary Tree (Java版; Easy) 题目描述 Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b...

littlehaes的博客 163

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 binary tree is the length of the longest path between any two nodes in a tree. This path may or

赵小越的博客 366
上一篇: LeetCode算法题-Reverse String II(Java实现)
下一篇: LeetCode算法题-Student Attendance Record I(Java实现)
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值