LeetCode #543 - Diameter of Binary Tree

LeetCode专项练习之树上深度优先搜索(Tree Depth-First Search)笔记 本文是根据穷码农的LeetCode刷题建议而进行专项练习时记录的心得。 这一次的训练加强了自己对递归的理解与熟练度,毕竟针对深度优先搜索,我是全部使用递归解决的问题。尽管许多题目我没能完全独立解决,需要解析进行辅助,但针对解析提供的方法我都进行了相对详细的分析,将思路的引导以笔记形式记录了下来,希望能对读者起到帮助。 今天的笔记包含基于树的深度优先搜索(Tree Depth-First S... 阅读详情

题目描述:

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

Note: The length of path between two nodes is represented by the number of edges between them.

求树中的最长路径长度,路径的定义为任意两个连通的节点之间的通路。

由于不一定经过根结点,所以从根结点递归的时候需要分经过根结节点和不经过根结点两种情况计算,同时如果经过根结点,那么为了求最长路径就需要求左子树的高度和右子树的高度,求树的高度也可以通过递归实现。

class Solution {
public:
    int diameterOfBinaryTree(TreeNode* root) {
        if (root==NULL||(root->left==NULL&&root->right==NULL)) return 0;
        int diameter=0;
        diameter=max(diameterOfBinaryTree(root->left),diameterOfBinaryTree(root->right));
        if(root->left==NULL||root->right==NULL)
            diameter=max(diameter,height_from_cur(root->left)+height_from_cur(root->right)+1);
        else diameter=max(diameter,height_from_cur(root->left)+height_from_cur(root->right)+2);
        return diameter;
    }
    
    int height_from_cur(TreeNode* cur){
        if (cur==NULL||(cur->left==NULL&&cur->right==NULL)) return 0;
        return max(height_from_cur(cur->left),height_from_cur(cur->right))+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 binary tree is the length of thelongestpath between any two nodes in a tree. This path may or may n... 阅读详情

相关推荐

使用LogisticRegression和LightGBM模型对信贷违约进行预测----基于kaggle比赛数据

3.分离数值型变量与类别型变量,发现有些数值型变量因为输入不规范,比如数值中含有字符(28_,_10000_等)被划分为类别型变量 ,比如 'Age','Annual_Income','Num_of_Loan', 'Num_of_Delayed_Payment', 'Changed_Credit_Limit','Credit_Mix','Monthly_Balance', 'Outstanding_Debt' ,'Amount_invested_monthly'等。三、分箱,woe编码,计算IV值。

I_am_a_big_tiger的博客 1693

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

ccystewart的博客 200

深度可分离卷积(DepthwiseSeparableConvolution):Depthwise卷积与Pointwise卷积

深度可分离卷积不用多说,在轻量级网络架构方面是一个绕不开的话题,只要接触深度学习多多少少会接触。深度可分离卷积即DepthwiseSeparableConvolution,该卷积将一个常规卷积过程划分成两个完成Depthwise卷积和Pointwise卷积,在保证输出一样时,计算量大大降低。首先来看,一个常规卷积是怎么实现的。其示意图如果我们从通道数变换的角度来看,卷积就是将3通道的tensor变为了4通道的tensor,我们来看看深度可分离卷积是怎么实现通道的变化的。...

xu.hyj 1万+

leetcode编程记录9 #543 Diameter of Binary Tree

leetcode编程记录9 #543 Diameter of Binary Tree标签(空格分隔): leetcode这次是一道关于树的题目,这是一个比较简单的题目,找出在树中的一条最长的路径的长度。题目如下:Given a binary tree, you need to compute the length of the diameter of the tree. The diameter o

grow_to的博客 204

【若依(ruoyi)】BootstrapTable列宽定制实战:突破width属性限制的两种方案

本文针对若依(RuoYi)框架中BootstrapTable组件列宽设置失效的常见问题,提供了两种实战解决方案。通过CSS样式覆盖法强制定义列宽,或利用formatter函数进行动态HTML包裹,有效突破width属性限制,实现精准的表格列宽定制。

weixin_29013129的博客 179

Wine7.22实战:如何在Ubuntu上完美运行通达信和微信(附字体配置技巧)

本文详细介绍了如何在Ubuntu系统上使用Wine7.22版本,完美运行通达信和微信这两款Windows专属软件。通过选择稳定的Wine7.22版本、利用现成的兼容容器,并重点解决了中文字体渲染的核心难题,提供了从环境搭建、字体配置到软件安装调优的一站式实战指南,帮助用户在Linux环境下无缝使用关键金融与通讯工具。

weixin_29280657的博客 569

LeetCode 543. Diameter of Binary Tree 题解

本题是二叉树的经典问题,主要考察对二叉树遍历的理解和应用。通过使用深度优先搜索,我们可以高效地计算二叉树的直径。深度优先搜索的核心思想是:对于每个节点,计算其左子树的深度和右子树的深度,该节点的直径为左子树深度 + 右子树深度,更新全局最大直径,返回该节点的深度。这种方法不仅适用于二叉树的直径问题,还可以应用于许多其他需要计算二叉树深度的场景。掌握深度优先搜索的思想,对于解决这类问题非常重要。

cannonjinx的博客 2959

2021年全网最细 VirtualBox 虚拟机安装 Ubuntu 20.04.2.0 LTS及Ubuntu的相关配置

目录一、VirtualBox 虚拟机的安装1. VirtualBox 的下载和安装2. 设置 VirtualBox 存储文件夹3. 在 VirtualBox 创建虚拟机二、Linux 操作系统(Ubuntu)的安装1. Linux 操作系统(Ubuntu)的下载2. 安装 Ubuntu3. 启动 Ubuntu三、配置 Ubuntu1. 设置终端2. 设置共享剪贴板3. 设置共享文件夹 一、VirtualBox 虚拟机的安装 VirtualBox 是由德国 Innotek 公司开发,由 Sun Microsy

XAMO Lab|代码、调试与复盘笔记 2万+

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

MDIO接口协议与其详解——单片机

在单片机领域,通过MDIO接口可以与外部设备进行通信,实现对设备的控制和数据的读写。通过本文的介绍,你可以了解到MDIO接口协议的原理和使用方法,并参考源代码示例进行实际的开发和应用。具体的初始化方法与单片机的型号和开发环境有关,可以参考相应的单片机手册和开发工具的文档。(1)发送读取命令:向MDIO线发送读取命令,通常是发送一个特定的控制字节,用于指示要读取的寄存器地址。(1)发送写入命令:向MDIO线发送写入命令,通常是发送一个特定的控制字节,用于指示要写入的寄存器地址。二、MDIO接口协议使用方法。

web_class282的博客 2634

(四)DDR协议命令波形时序一——(ACT、Read、Write)

文章目录一、DDR命令真值表二、ACT激活命令时序要求2.1、ACTIVATE波形——tRRD、tFAW2.2、ACTIVATE波形——tRAS、tRC三、Read命令时序要求3.1、Read 波形——无Additive Latency3.2、Read 波形——有Additive Latency3.3、Read 波形——Additive Latency使用场景 一、DDR命令真值表 DDR的Command命令由CKE、CS#、RAS#、CAS#、WE#信号组成,这几个信号进行不同的搭配组合会形成不同的Co

Mr.翟的博客 6893

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

卷卷萌的博客 265

华为OD面试八股文嵌入式C语言基础问题及答案(一)

本文总结了C语言编程中的10个关键知识点:1.宏定义MIN的实现方法;2.嵌入式无限循环的两种写法;3.变量a的8种不同定义方式;4.static关键字的三种作用;5.const关键字的含义和使用场景;6.volatile关键字的含义及三个应用实例;7.变量存储位置(栈区、静态区、堆区);8.32位系统指针位数;9.局部与全局变量重名规则;10.引用与指针的三点区别。这些内容涵盖了宏定义、变量声明、存储类别、指针等C语言核心概念,对嵌入式开发和系统编程具有重要参考价值。

m0_50382756的博客 167

[leetcode]543. Diameter of Binary Tree

题目链接:https://leetcode.com/problems/diameter-of-binary-tree/#/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 leng

xiaocong1990的博客 285

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

IEM模型,地表粗糙度模拟

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

[LeetCode]543. Diameter of Binary Tree

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

gqk289的专栏 1979

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

leetcode - 543. Diameter of Binary Tree

leetcode - 543

sinat_41679123的博客 583

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

[leetcode] 543. Diameter of Binary Tree @ python

原题 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 ma...

闲庭信步 540

[leetcode] 543. Diameter of Binary Tree

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

Learning from the mistakes 245

[LeetCode] 543. Diameter of Binary Tree

题目内容 https://leetcode-cn.com/problems/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 thelon...

Reyiiiin的深夜一点半 103

LeetCode 543. Diameter of Binary Tree

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

Infi_zc 143

LeetCode 543. Diameter of Binary Tree 解题笔记

1. 题目描述https://leetcode.com/problems/diameter-of-binary-tree/#/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

zhyh1435589631的专栏 3079

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

djph26741的博客 96

leetcode(543). Diameter of Binary Tree

problem 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 pa

qishi的博客 473

Leetcode 543. Diameter of Binary Tree

【代码】Leetcode 543. Diameter of Binary Tree。

taoqick的专栏 418

[leetcode: Python]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 may no

cyy2learn的博客 2088

【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

【LeetCode】Diameter of Binary Tree 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/diameter-of-binary-tree/#/description题目描述:Given a binary tree, you need to compute the length of the diameter

负雪明烛 2568
上一篇: LeetCode #538 - Convert BST to Greater Tree
下一篇: LeetCode #572 - Subtree of Another Tree
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址