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

阿里云网站备案入口电商seo与sem是什么

阿里云网站备案入口,电商seo与sem是什么,号卡分销系统,现在手机网站用什么做的题目 输入一棵二叉树,请找出二叉树中每层的最大值。例如,输入图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://www.hkea.cn/news/753196/

相关文章:

  • 湖南做网站的公司有哪些搜索引擎是什么
  • flash网站管理系统seo优化排名易下拉用法
  • 永年网站建设友链互换平台推荐
  • 企业网站的设计公司网络广告营销的典型案例
  • 高校思政主题网站建设的意义关键词歌词任然
  • 哪里做网站比较快2345网址导航下载桌面
  • 广州建设委员会官方网站凡科建站下载
  • 全球做网站的公司排名百度一下你就知道官网
  • 小企业网站价格免费发链接的网站
  • 买了空间和域名 怎么做网站哪家公司网站做得好
  • 网站备案是否关闭衡阳网站建设公司
  • 遂昌建设局网站个人怎么做网站
  • 软件开发和网站建设网络营销的未来6个发展趋势
  • 做网站一年多少钱免费seo网站推广
  • 智通人才网东莞最新招聘信息官网seo是如何做优化的
  • 个人做跨境电商网站百度地图导航手机版免费下载
  • 阿里云注册网站之后怎么做网站百度联盟是什么
  • 动画制作视频河南网站排名优化
  • 网站关键词怎么做排名掌门一对一辅导官网
  • 现在什么网站做推广比较好网页设计需要学什么
  • 个人购物网站 怎么建网络营销包括
  • 有没有做鸭的网站工作室招聘广州网站优化工具
  • 深圳营销外深圳网络营销公司seo和sem的联系
  • 专业的网站制作公司哪家好竞价专员是做什么的
  • 海南省建设厅网站百度seo霸屏软件
  • 淄博张店做网站的公司爱站小工具圣经
  • wordpress w3seo优化自学
  • 临沂手机建站模板微信seo排名优化软件
  • 网站管理员怎么做板块建设艺人百度指数排行榜
  • 如何创建企业网站网络舆情处置的五个步骤