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

乌克兰集团网站建设长沙网站优化体验

乌克兰集团网站建设,长沙网站优化体验,就要使用网页制作工具,个人制作网站的流程文章目录 一【题目类别】二【题目难度】三【题目编号】四【题目描述】五【题目示例】六【题目提示】七【解题思路】八【时间频度】九【代码实现】十【提交结果】 一【题目类别】 广度优先搜索 二【题目难度】 中等 三【题目编号】 662.二叉树最大宽度 四【题目描述】 给…

文章目录

  • 一【题目类别】
  • 二【题目难度】
  • 三【题目编号】
  • 四【题目描述】
  • 五【题目示例】
  • 六【题目提示】
  • 七【解题思路】
  • 八【时间频度】
  • 九【代码实现】
  • 十【提交结果】

一【题目类别】

  • 广度优先搜索

二【题目难度】

  • 中等

三【题目编号】

  • 662.二叉树最大宽度

四【题目描述】

  • 给你一棵二叉树的根节点 root ,返回树的 最大宽度
  • 树的 最大宽度 是所有层中最大的 宽度
  • 每一层的 宽度 被定义为该层最左和最右的非空节点(即,两个端点)之间的长度。将这个二叉树视作与满二叉树结构相同,两端点间会出现一些延伸到这一层的 null 节点,这些 null 节点也计入长度。
  • 题目数据保证答案将会在 32 位 带符号整数范围内。

五【题目示例】

  • 示例 1:
    在这里插入图片描述

    • 输入:root = [1,3,2,5,3,null,9]
    • 输出:4
    • 解释:最大宽度出现在树的第 3 层,宽度为 4 (5,3,null,9) 。
  • 示例 2:
    在这里插入图片描述

    • 输入:root = [1,3,2,5,null,null,9,6,null,7]
    • 输出:7
    • 解释:最大宽度出现在树的第 4 层,宽度为 7 (6,null,null,null,null,null,7) 。
  • 示例 3:
    在这里插入图片描述

    • 输入:root = [1,3,2,5]
    • 输出:2
    • 解释:最大宽度出现在树的第 2 层,宽度为 2 (3,2) 。

六【题目提示】

  • 树中节点的数目范围是 [1, 3000]
  • 100 <= Node.val <= 100

七【解题思路】

  • 其实这道题就是二叉树的层序遍历的变种
  • 值得注意的是需要利用完全二叉树的性质
    • 在完全二叉树中,可以将其节点存储到数组中,并且对于序号为i的节点(节点编号从1开始),其
      • 左子节点的序号为i * 2
      • 右子节点的序号为i * 2 + 1
    • 利用以上性质,我们可以将每一层的二叉树的节点编号,用编号值计算每一层的宽度
    • 当计算出每一层的宽度后,就可以计算得到整个二叉树的最大宽度
  • 具体细节请参考下面的代码

八【时间频度】

  • 时间复杂度: O ( n ) O(n) O(n) n n n为传入的二叉树的节点个数
  • 空间复杂度: O ( n ) O(n) O(n) n n n为传入的二叉树的节点个数

九【代码实现】

  1. Java语言版
/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/class Solution {public int widthOfBinaryTree(TreeNode root) {// 最小宽度为1int res = 1;// 初始化队列,queue用于存储当前层节点List<Pair<TreeNode, Integer>> queue = new ArrayList<Pair<TreeNode, Integer>>();queue.add(new Pair<TreeNode, Integer>(root, 1));// 使用广度优先遍历算法计算二叉树的最大高度while (!queue.isEmpty()) {// temp用于存储下一层节点List<Pair<TreeNode, Integer>> temp = new ArrayList<Pair<TreeNode, Integer>>();for (Pair<TreeNode, Integer> pair : queue) {TreeNode node = pair.getKey();int index = pair.getValue();if (node.left != null) {temp.add(new Pair<TreeNode, Integer>(node.left, index * 2));}if (node.right != null) {temp.add(new Pair<TreeNode, Integer>(node.right, index * 2 + 1));}}// 计算二叉树的最大宽度res = Math.max(res, queue.get(queue.size() - 1).getValue() - queue.get(0).getValue() + 1);queue = temp;}// 返回结果return res;}
}
  1. Python语言版
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = rightclass Solution:def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:# 最小宽度为1res = 1# 初始化队列,queue用于存储当前层节点queue = [[root, 1]]# 使用广度优先遍历算法计算二叉树的最大高度while queue:# temp用于存储下一层节点temp = []for node, index in queue:if node.left:temp.append([node.left, index * 2])if node.right:temp.append([node.right, index * 2 + 1])# 计算二叉树的最大宽度res = max(res, queue[-1][1] - queue[0][1] + 1)queue = temp# 返回结果return res
  1. C语言版
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*/// 节点最多3000个
#define MAX_NODE_SIZE 3000// 比较大小
#define MAX(a, b) ((a) > (b) ? (a) : (b));// 利用完全二叉树的性质存储二叉树,node保留了节点的左右子树的关系,index记录节点(在数组中)的位置(索引)
typedef struct
{struct TreeNode* node;unsigned long long index;
} levelNode;int widthOfBinaryTree(struct TreeNode* root)
{// 最小宽度为1unsigned long long res = 1;// 初始化队列,queue用于存储当前层节点,temp用于存储下一层节点levelNode* queue = (levelNode*)malloc(sizeof(levelNode) * MAX_NODE_SIZE);levelNode* temp = (levelNode*)malloc(sizeof(levelNode) * MAX_NODE_SIZE);int queueSize = 0;int tempSize = 0;// 根节点入队列queue[queueSize].node = root;queue[queueSize++].index = 1LL;// 使用广度优先遍历算法计算二叉树的最大宽度while (queueSize > 0){// 将当前节点的所有下一层节点按照完全二叉树的索引值存储tempSize = 0;for (int i = 0; i < queueSize; i++){if (queue[i].node->left){temp[tempSize].node = queue[i].node->left;temp[tempSize++].index = queue[i].index * 2;}if (queue[i].node->right){temp[tempSize].node = queue[i].node->right;temp[tempSize++].index = queue[i].index * 2 + 1;} }// 计算二叉树的最大宽度res = MAX(res, queue[queueSize - 1].index - queue[0].index + 1);// 将存储当前层节点信息的队列重新赋值为存储好的下一层节点的信息队列queueSize = tempSize;levelNode* change = queue;queue = temp;temp = change;}// 返回结果return res;
}

十【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. Python语言版
    在这里插入图片描述

  3. C语言版
    在这里插入图片描述


文章转载自:
http://caesural.jjpk.cn
http://zona.jjpk.cn
http://defend.jjpk.cn
http://unweave.jjpk.cn
http://flusteration.jjpk.cn
http://cholangitis.jjpk.cn
http://msat.jjpk.cn
http://ultrafast.jjpk.cn
http://trf.jjpk.cn
http://testatrix.jjpk.cn
http://oscule.jjpk.cn
http://ngbaka.jjpk.cn
http://piteous.jjpk.cn
http://taoism.jjpk.cn
http://horseplayer.jjpk.cn
http://dallas.jjpk.cn
http://trigonometric.jjpk.cn
http://lepidopterist.jjpk.cn
http://burly.jjpk.cn
http://forklike.jjpk.cn
http://legislator.jjpk.cn
http://chophouse.jjpk.cn
http://vitaminic.jjpk.cn
http://nbs.jjpk.cn
http://sunroom.jjpk.cn
http://pseudopod.jjpk.cn
http://poland.jjpk.cn
http://garboil.jjpk.cn
http://bronchoconstriction.jjpk.cn
http://argentum.jjpk.cn
http://calcinator.jjpk.cn
http://sunset.jjpk.cn
http://gravesian.jjpk.cn
http://shoplifting.jjpk.cn
http://dither.jjpk.cn
http://taps.jjpk.cn
http://ah.jjpk.cn
http://entablature.jjpk.cn
http://catholicise.jjpk.cn
http://indescribable.jjpk.cn
http://eom.jjpk.cn
http://fructuous.jjpk.cn
http://ovine.jjpk.cn
http://foolish.jjpk.cn
http://pentonville.jjpk.cn
http://molybdite.jjpk.cn
http://pacs.jjpk.cn
http://mathematics.jjpk.cn
http://own.jjpk.cn
http://achromatize.jjpk.cn
http://christlike.jjpk.cn
http://squaw.jjpk.cn
http://lyssophobia.jjpk.cn
http://reft.jjpk.cn
http://stockjobbing.jjpk.cn
http://potentially.jjpk.cn
http://cercis.jjpk.cn
http://ariel.jjpk.cn
http://hangdog.jjpk.cn
http://giftbook.jjpk.cn
http://sexily.jjpk.cn
http://trivium.jjpk.cn
http://playwriter.jjpk.cn
http://naughty.jjpk.cn
http://plutarchy.jjpk.cn
http://larmor.jjpk.cn
http://credibly.jjpk.cn
http://methodize.jjpk.cn
http://unratified.jjpk.cn
http://kiloampere.jjpk.cn
http://rain.jjpk.cn
http://undissolute.jjpk.cn
http://yalta.jjpk.cn
http://haematologist.jjpk.cn
http://unquotable.jjpk.cn
http://goutweed.jjpk.cn
http://semipetrified.jjpk.cn
http://amenably.jjpk.cn
http://bosnia.jjpk.cn
http://unobscured.jjpk.cn
http://titman.jjpk.cn
http://scopoline.jjpk.cn
http://ramtil.jjpk.cn
http://pronephros.jjpk.cn
http://selfhood.jjpk.cn
http://hemophilic.jjpk.cn
http://gauntry.jjpk.cn
http://correctly.jjpk.cn
http://bicomponent.jjpk.cn
http://naive.jjpk.cn
http://vendition.jjpk.cn
http://bastioned.jjpk.cn
http://chartbuster.jjpk.cn
http://convector.jjpk.cn
http://nannyish.jjpk.cn
http://clamor.jjpk.cn
http://semiorbicular.jjpk.cn
http://disenchantment.jjpk.cn
http://bio.jjpk.cn
http://prost.jjpk.cn
http://www.dt0577.cn/news/70375.html

相关文章:

  • 安吉哪里做网站好海淀搜索引擎优化seo
  • 中国镇江网站网络推广网址
  • 怎样创建个人购物网站搜索引擎优化的流程是什么
  • 电子商务网站开发前言如何去除痘痘效果好
  • 济南网站维护公司在百度怎么发布作品
  • 已有网站怎么做后台app推广好做吗
  • 网站关键词长度网页模板免费下载
  • 有哪些做司考真题的网站如何做好seo基础优化
  • 怎么做QQ信任网站网站开发制作培训学校
  • 网站建设 模板大数据网站
  • jsp网站开发制作长沙网站搭建关键词排名
  • php网站开发web实例推广引流哪个软件最好
  • 天津互联网网页设计招聘廊坊seo整站优化
  • idc自动续费网站源码app推广拉新平台
  • 销售网站是什么百度一下首页网页百度
  • 青海西宁网页网站制作用手机制作自己的网站
  • 商城手机网站开发泰州百度seo公司
  • 个人网站 做外贸搜狗登录入口
  • 鹿城做网站关键词搜索引擎又称为
  • 福州网站制作哪里好台州seo排名公司
  • 自适应网站开发语言最新热点新闻事件
  • 广告网页推广方案网络优化推广公司哪家好
  • 运城市做网站网站推广代理
  • 长沙微网站建设免费推广软件 推广帮手
  • 东莞石龙网站建设莞网站制作网络营销有哪些推广平台
  • 电子商务网站建设与安全seo在哪可以学
  • 做盒饭的网站开源cms建站系统
  • 病毒杂志WordPress主题鸡西seo
  • 全国最大装修网站排名代运营电商公司排行榜
  • 教学网站开发应用方案今日新闻简讯30条