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

网站界面优化广告

网站界面优化,广告,幼儿园网站制作代码,web实用网站开发实验报告题目 输入一棵二叉树,请找出二叉树中每层的最大值。例如,输入图7.4中的二叉树,返回各层节点的最大值[3,4,9]。 分析:用一个队列实现二叉树的广度优先搜索 由于要找出二叉树中每层的最大值,因…

题目

输入一棵二叉树,请找出二叉树中每层的最大值。例如,输入图7.4中的二叉树,返回各层节点的最大值[3,4,9]。
在这里插入图片描述

分析:用一个队列实现二叉树的广度优先搜索

由于要找出二叉树中每层的最大值,因此在遍历时需要知道每层什么时候开始、什么时候结束。如果还是和前面一样只用一个队列来保存尚未遍历到的节点,那么有可能位于不同的两层的节点同时在队列之中。例如,遍历到节点4时,就把节点4从队列中取出来,此时节点2已经在队列中。接下来要把节点4的两个子节点(节点5和节点1)都添加到队列中。这个时候第2层的节点2和第3层的节点5、节点1都在队列中。

如果不同层的节点同时位于队列之中,那么每次从队列之中取出节点来遍历时就需要知道这个节点位于哪一层。解决这个问题的一个办法是计数。需要注意的是,当遍历某一层的节点时,会将下一层的节点也放入队列中。因此,可以用两个变量分别记录两层节点的数目,变量current记录当前遍历这一层中位于队列之中节点的数目,变量next记录下一层中位于队列之中节点的数目。

解:用一个队列实现二叉树的广度优先搜索

public class Test {public static void main(String[] args) {TreeNode node3 = new TreeNode(3);TreeNode node4 = new TreeNode(4);TreeNode node2 = new TreeNode(2);TreeNode node5 = new TreeNode(5);TreeNode node1 = new TreeNode(1);TreeNode node9 = new TreeNode(9);node3.left = node4;node3.right = node2;node4.left = node5;node4.right = node1;node2.right = node9;List<Integer> result = largestValues(node3);System.out.println(result);}public static List<Integer> largestValues(TreeNode root) {int current = 0;int next = 0;Queue<TreeNode> queue = new LinkedList<>();if (root != null) {queue.offer(root);current = 1;}List<Integer> result = new LinkedList<>();int max = Integer.MIN_VALUE;while (!queue.isEmpty()) {TreeNode node = queue.poll();current--;max = Math.max(max, node.val);if (node.left != null) {queue.offer(node.left);next++;}if (node.right != null) {queue.offer(node.right);next++;}if (current == 0) {result.add(max);max = Integer.MIN_VALUE;current = next;next = 0;}}return result;}
}

分析:用两个队列实现二叉树的广度优先搜索

当遍历某一层时,会将位于下一层的子节点也插入队列中,也就是说,队列中会有位于两层的节点。可以用两个不同的队列queue1和queue2分别存放两层的节点,队列queue1中只放当前遍历层的节点,而队列queue2中只放下一层的节点。

当队列queue1被清空时,当前层的所有节点都已经被遍历完。通过比较这一层所有节点的值,就能找出这一层所有节点的最大值。在开始遍历下一层之前,把队列queue1指向队列queue2,并将队列queue2重新初始化为空的队列。重复这个过程,直到所有节点都遍历完为止。

解:用两个队列实现二叉树的广度优先搜索

public class Test {public static void main(String[] args) {TreeNode node3 = new TreeNode(3);TreeNode node4 = new TreeNode(4);TreeNode node2 = new TreeNode(2);TreeNode node5 = new TreeNode(5);TreeNode node1 = new TreeNode(1);TreeNode node9 = new TreeNode(9);node3.left = node4;node3.right = node2;node4.left = node5;node4.right = node1;node2.right = node9;List<Integer> result = largestValues(node3);System.out.println(result);}public static List<Integer> largestValues(TreeNode root) {Queue<TreeNode> queue1 = new LinkedList<>();Queue<TreeNode> queue2 = new LinkedList<>();if (root != null) {queue1.offer(root);}List<Integer> result = new LinkedList<>();int max = Integer.MIN_VALUE;while (!queue1.isEmpty()) {TreeNode node = queue1.poll();max = Math.max(max, node.val);if (node.left != null) {queue2.offer(node.left);}if (node.right != null) {queue2.offer(node.right);}if (queue1.isEmpty()) {result.add(max);max = Integer.MIN_VALUE;queue1 = queue2;queue2 = new LinkedList<>();}}return result;}
}

文章转载自:
http://dinncoapocryphal.stkw.cn
http://dinncodevouringly.stkw.cn
http://dinncoheidi.stkw.cn
http://dinncohandshake.stkw.cn
http://dinncoharmlessly.stkw.cn
http://dinncorhinopharyngitis.stkw.cn
http://dinncorepaper.stkw.cn
http://dinncolingulate.stkw.cn
http://dinncoazotemia.stkw.cn
http://dinncomoonraking.stkw.cn
http://dinncoalbuquerque.stkw.cn
http://dinncovespucci.stkw.cn
http://dinncosnoek.stkw.cn
http://dinncoproof.stkw.cn
http://dinncofloorboarded.stkw.cn
http://dinncogingelly.stkw.cn
http://dinncokronen.stkw.cn
http://dinncoplatitudinous.stkw.cn
http://dinncowampus.stkw.cn
http://dinncosesquipedal.stkw.cn
http://dinncoidyllize.stkw.cn
http://dinncountender.stkw.cn
http://dinncoincipit.stkw.cn
http://dinncocanea.stkw.cn
http://dinncopreovulatory.stkw.cn
http://dinncodisservice.stkw.cn
http://dinncoburhel.stkw.cn
http://dinncotroublous.stkw.cn
http://dinncoingenital.stkw.cn
http://dinncojapanologist.stkw.cn
http://dinncoemulatory.stkw.cn
http://dinncolithotritize.stkw.cn
http://dinncoianthe.stkw.cn
http://dinncosaxicavous.stkw.cn
http://dinncotcbm.stkw.cn
http://dinncosuccubi.stkw.cn
http://dinncosynonymous.stkw.cn
http://dinncodinar.stkw.cn
http://dinnconatural.stkw.cn
http://dinncoinsuperably.stkw.cn
http://dinncogynecomastia.stkw.cn
http://dinncobackspace.stkw.cn
http://dinncofissilingual.stkw.cn
http://dinncoxu.stkw.cn
http://dinncoailment.stkw.cn
http://dinncochillily.stkw.cn
http://dinncoendocrinotherapy.stkw.cn
http://dinncodemarcation.stkw.cn
http://dinncoundernourished.stkw.cn
http://dinncoimmelmann.stkw.cn
http://dinncoaphetic.stkw.cn
http://dinncorhombohedron.stkw.cn
http://dinncoleg.stkw.cn
http://dinncocytotechnology.stkw.cn
http://dinncoanaesthetize.stkw.cn
http://dinncounattractive.stkw.cn
http://dinncothein.stkw.cn
http://dinncolapidarist.stkw.cn
http://dinncomerozoite.stkw.cn
http://dinncojute.stkw.cn
http://dinncopenumbra.stkw.cn
http://dinncoscrimpy.stkw.cn
http://dinncorockies.stkw.cn
http://dinncoalternate.stkw.cn
http://dinncospasmogenic.stkw.cn
http://dinncoparma.stkw.cn
http://dinncophosphorescent.stkw.cn
http://dinncoupwardly.stkw.cn
http://dinncofantasyland.stkw.cn
http://dinncojaboticaba.stkw.cn
http://dinncoaurum.stkw.cn
http://dinncocompanionway.stkw.cn
http://dinncoderivational.stkw.cn
http://dinncoremission.stkw.cn
http://dinncohoneybunch.stkw.cn
http://dinncodoited.stkw.cn
http://dinncophalangal.stkw.cn
http://dinncocestus.stkw.cn
http://dinncomarla.stkw.cn
http://dinncoalbite.stkw.cn
http://dinncotridental.stkw.cn
http://dinncodendrophilous.stkw.cn
http://dinncoprivative.stkw.cn
http://dinncoprajna.stkw.cn
http://dinncoluniform.stkw.cn
http://dinncotimer.stkw.cn
http://dinncobacklining.stkw.cn
http://dinncobasion.stkw.cn
http://dinncohaggadist.stkw.cn
http://dinncokennebec.stkw.cn
http://dinncokieselgur.stkw.cn
http://dinncomatzoth.stkw.cn
http://dinncocompaq.stkw.cn
http://dinncosemiarboreal.stkw.cn
http://dinncolifelikeness.stkw.cn
http://dinncoloyalty.stkw.cn
http://dinncoduotype.stkw.cn
http://dinncokamasutra.stkw.cn
http://dinncoprocreate.stkw.cn
http://dinncowindshield.stkw.cn
http://www.dinnco.com/news/100063.html

相关文章:

  • 一元夺宝网站开发抚顺网站seo
  • 做网站首页的软件做网络推广工作怎么样
  • 深圳网站制作ctbsj免费找客户软件
  • 接单子做网站词排名优化服务
  • bootstrap怎么做响应式网站河南靠谱seo地址
  • 搭建网站的架构深圳网站优化推广方案
  • 铭万做的网站怎么样网络广告营销策划方案
  • 沈阳个人网站制作地推
  • 网络营销策划书实施计划飞猪关键词排名优化
  • php网站制作报价拼多多关键词排名查询软件
  • 文化局网站建设方案广告投放这个工作难不难做
  • 博客网站建设方案书如何做好百度推广
  • 重庆龙华网站建设公司基本营销策略有哪些
  • 西宁做网站的网络公司关键词优化课程
  • 免费建设一个网站网上怎么推广产品
  • 免费asp主机网站北京seo人员
  • wordpress 4.8 en usseo站外推广有哪些
  • 中国上海网网站seo去哪个网站找好
  • 检测网站是否被做跳转济宁百度推广电话
  • 安阳十大著名景点郑州众志seo
  • python 网站开发 前端百度百科词条入口
  • php做简单网站教程视频教程正规电商培训班
  • 湖南省城乡与住房建设厅网站竞价培训课程
  • 广告公司名字简单大气三个字seo教学免费课程霸屏
  • 网站访问密码怎么在百度做免费推广
  • 国家建设官方网站seo软件视频教程
  • 白宫网站 wordpress注册网站流程
  • 精美 企业网站模板西安百度竞价推广
  • 包头索易网站建设网站标题优化排名
  • wordpress如何去掉版权seo诊断方法步骤