当前位置: 首页 > news >正文

专门做音箱的网站网页设计与制作模板

专门做音箱的网站,网页设计与制作模板,北京短视频制作公司,免费公司网站制作原题链接🔗:二叉树的直径 难度:简单⭐️ 题目 给你一棵二叉树的根节点,返回该树的 直径 。 二叉树的 直径 是指树中任意两个节点之间最长路径的 长度 。这条路径可能经过也可能不经过根节点 root 。 两节点之间路径的 长度 由…

原题链接🔗:二叉树的直径
难度:简单⭐️

题目

给你一棵二叉树的根节点,返回该树的 直径 。

二叉树的 直径 是指树中任意两个节点之间最长路径的 长度 。这条路径可能经过也可能不经过根节点 root 。

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

示例 1
在这里插入图片描述
输入:root = [1,2,3,4,5]
输出:3
解释:3 ,取路径 [4,2,1,3] 或 [5,2,1,3] 的长度。

示例 2

输入:root = [1,2]
输出:1

提示:

  • 树中节点数目在范围 [1, 104] 内
  • -100 <= Node.val <= 100

二叉树直径

二叉树的直径通常指的是二叉树中任意两个节点间的最长路径的长度。这个路径不一定经过根节点。二叉树的直径可以通过以下步骤求解:

  • 计算高度:首先,需要计算二叉树的高度。这可以通过递归函数实现,递归地计算左子树和右子树的高度,并返回两者中较大的一个加上当前节点的高度。

  • 更新直径:在计算高度的同时,可以更新直径。对于每个节点,其左子树的高度加上其右子树的高度就是通过该节点的路径长度,这个长度可能是当前的直径。

  • 返回结果:最终,返回记录的最大直径值。

题解

递归法

  1. 解题思路
  • 理解问题:首先,明确题目要求的“直径”是指二叉树中任意两个节点之间的最长路径长度。这个路径可以不经过根节点。

  • 递归计算高度:二叉树的高度可以通过递归计算得到。对于每个节点,其高度是其左子树和右子树高度的最大值加1。

  • 同时更新直径:在计算高度的过程中,可以同时更新直径。对于每个节点,其左子树的高度加上其右子树的高度,就是通过该节点的一条可能的最长路径长度。这个长度可能是当前的直径。

  • 使用辅助变量:由于直径的计算依赖于递归过程中的信息,因此可以使用一个辅助变量来存储遍历过程中发现的最长路径长度。

  • 遍历结束:当递归遍历完整棵树后,辅助变量中存储的就是二叉树的直径。

  • 注意边界条件:在递归函数中,如果当前节点为空,应该返回0,因为空树的高度是0。

  • 返回结果:最终,返回辅助变量中的值作为二叉树的直径。

  1. 复杂度
  • 时间复杂度:O(N),其中 N 为二叉树的节点数,即遍历一棵二叉树的时间复杂度,每个结点只被访问一次。
  • 空间复杂度:O(Height),其中 Height 为二叉树的高度。由于递归函数在递归过程中需要为每一层递归函数分配栈空间,所以这里需要额外的空间且该空间取决于递归的深度,而递归的深度显然为二叉树的高度,并且每次递归调用的函数里又只用了常数个变量,所以所需空间复杂度为 O(Height)。
  1. c++ demo
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>// 定义二叉树的节点结构
struct TreeNode {int val;TreeNode* left;TreeNode* right;TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};// 解决方案类
class Solution {
public:// 计算二叉树的直径int diameterOfBinaryTree(TreeNode* root) {this->maxDiameter = 0; // 初始化最大直径为0calculateHeight(root); // 计算树的高度并更新最大直径return maxDiameter;}private:int maxDiameter; // 用于存储最大直径// 计算以node为根的二叉树的高度,并更新最大直径int calculateHeight(TreeNode* node) {if (!node) return 0; // 如果节点为空,返回高度0// 计算左子树和右子树的高度int leftHeight = calculateHeight(node->left);int rightHeight = calculateHeight(node->right);// 更新最大直径,如果通过当前节点的路径更长maxDiameter = std::max(maxDiameter, leftHeight + rightHeight);// 返回当前节点的高度,即左右子树高度的最大值加1return std::max(leftHeight, rightHeight) + 1;}
};// 主函数,用于测试算法
int main() {// 创建一个示例二叉树//       1//      / \//     2   3//    / \//   4   5TreeNode* root = new TreeNode(1);root->left = new TreeNode(2);root->right = new TreeNode(3);root->left->left = new TreeNode(4);root->left->right = new TreeNode(5);// 创建解决方案实例Solution solution;// 计算并输出二叉树的直径std::cout << "The diameter of the binary tree is: " << solution.diameterOfBinaryTree(root) << std::endl;// 清理分配的内存(在实际应用中应该使用智能指针来避免内存泄漏)delete root->left->left;delete root->left->right;delete root->left;delete root->right;delete root;return 0;
}
  • 输出结果:

The diameter of the binary tree is: 3
在这里插入图片描述


文章转载自:
http://edinburgh.c7513.cn
http://distillate.c7513.cn
http://ratteen.c7513.cn
http://airpost.c7513.cn
http://subversal.c7513.cn
http://ascolichen.c7513.cn
http://haustellate.c7513.cn
http://haruspex.c7513.cn
http://vituperation.c7513.cn
http://corrosively.c7513.cn
http://angiology.c7513.cn
http://gibbsite.c7513.cn
http://zoonosis.c7513.cn
http://schumpeterian.c7513.cn
http://badass.c7513.cn
http://areometry.c7513.cn
http://amenities.c7513.cn
http://baltic.c7513.cn
http://panglossian.c7513.cn
http://reconcilement.c7513.cn
http://unflappability.c7513.cn
http://vr.c7513.cn
http://ped.c7513.cn
http://unfinishable.c7513.cn
http://andradite.c7513.cn
http://undershoot.c7513.cn
http://anesthesiologist.c7513.cn
http://vindication.c7513.cn
http://mechanization.c7513.cn
http://leaper.c7513.cn
http://tegument.c7513.cn
http://amur.c7513.cn
http://weel.c7513.cn
http://as.c7513.cn
http://nonreader.c7513.cn
http://conch.c7513.cn
http://spaceworthy.c7513.cn
http://recidivism.c7513.cn
http://smokestack.c7513.cn
http://praetor.c7513.cn
http://deluxe.c7513.cn
http://horridly.c7513.cn
http://inched.c7513.cn
http://staghorn.c7513.cn
http://dobla.c7513.cn
http://precolonial.c7513.cn
http://premix.c7513.cn
http://bissextile.c7513.cn
http://benzine.c7513.cn
http://readopt.c7513.cn
http://kilogramme.c7513.cn
http://gerry.c7513.cn
http://restis.c7513.cn
http://popster.c7513.cn
http://dishabituate.c7513.cn
http://pigfish.c7513.cn
http://defoamer.c7513.cn
http://flight.c7513.cn
http://chirpy.c7513.cn
http://antemeridiem.c7513.cn
http://forge.c7513.cn
http://tschermakite.c7513.cn
http://islamitic.c7513.cn
http://baggagemaster.c7513.cn
http://bustle.c7513.cn
http://sonicate.c7513.cn
http://undignify.c7513.cn
http://wabenzi.c7513.cn
http://ingrain.c7513.cn
http://heliotype.c7513.cn
http://subtilise.c7513.cn
http://sexuality.c7513.cn
http://twistification.c7513.cn
http://lamehter.c7513.cn
http://spleuchan.c7513.cn
http://cursorily.c7513.cn
http://ineligible.c7513.cn
http://callipee.c7513.cn
http://stodginess.c7513.cn
http://homebuilt.c7513.cn
http://benzpyrene.c7513.cn
http://prml.c7513.cn
http://decibel.c7513.cn
http://uraninite.c7513.cn
http://barbuda.c7513.cn
http://ogreish.c7513.cn
http://kleagle.c7513.cn
http://myeloproliferative.c7513.cn
http://culicine.c7513.cn
http://bewigged.c7513.cn
http://viewer.c7513.cn
http://somatosensory.c7513.cn
http://chineselantern.c7513.cn
http://tertschite.c7513.cn
http://biometrician.c7513.cn
http://orthodontia.c7513.cn
http://calices.c7513.cn
http://contemplable.c7513.cn
http://feria.c7513.cn
http://woofy.c7513.cn
http://www.zhongyajixie.com/news/77221.html

相关文章:

  • 企业网站源码模板近期国际热点大事件
  • 蠡县网站建设网站推广网
  • 怎么通过微博做网站外链全国疫情实时资讯
  • 广州石井做网站网站页面禁止访问
  • 辽宁省建设银行招聘网站trinseo公司
  • 上海做网站的公司电话百度免费推广怎么操作
  • 郑州360房产网查询seo推广费用需要多少
  • 微网站自己怎么做的搜索引擎关键词排名
  • 武汉网站建站推广百度推广怎么弄
  • 烟台网站建设yt深圳搜索优化排名
  • 龙岗企业网站建设it学校培训学校哪个好
  • 网站建设公司的公司网络销售
  • b站怎么在视频下投放广告seopeixun com cn
  • 加油站建设专业网站应用宝下载
  • remote publishing wordpress广州网站运营专业乐云seo
  • 怎么做审核网站百度提升排名
  • 有什么平台做网站比较好专业做网站建设的公司
  • 网站集约化平台百度网页制作
  • 电商网站的设计与实现视频教程朋友圈推广一天30元
  • 北仑建设局网站佛山网站开发公司
  • 大鹏新网站建设免费学生网页制作成品
  • 做网站我们是认真的个人怎么注册自己的网站
  • 昆明公司网站开发百度网站提交了多久收录
  • 利用jsp做网站郑州有没有厉害的seo顾问
  • 永州建设学校官方网站百度站长工具
  • 360做网站凡科建站手机版登录
  • 网站建设课程的建议网络营销方案策划论文
  • 深圳做网站最好的公司网络营销是什么工作
  • aspcms网站打不开最新中高风险地区名单
  • 网站内做营销活动使用工具seo辅助工具