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

建筑工程类网站平顶山哪里做网站

建筑工程类网站,平顶山哪里做网站,专业从事成都网站建设,网站的好处编程导航第六关——白银挑战 树的层次遍历 LeetCode102 题目要求#xff1a;给你一个二叉树#xff0c;请你返回其按层序遍历得到的节点值。(即逐层地#xff0c;从左到右访问所有节点)。思路#xff1a; 我们根据队列的特点#xff0c;先进先出#xff1b;要实现全部节…编程导航第六关——白银挑战 树的层次遍历 LeetCode102 题目要求给你一个二叉树请你返回其按层序遍历得到的节点值。(即逐层地从左到右访问所有节点)。思路 我们根据队列的特点先进先出要实现全部节点的层次遍历再次我们采用队列的数据结构当队列中元素为空时则代表树已被遍历完成所以我们在循环时的条件就是quene.size0我们首先弹出元素然后将该节点的子节点压入队列当中这是队列中的出队顺序就是层次遍历的顺序在本题中我们需要实现每一层元素值的分别记录所以需要得到每一层对应的节点数是多少每一次的节点数就是队列的元素个数只要出队便是size-1同时在出队时会将子节点压入队列中当size0时那么这一层的元素已全部遍历完同时此时队列中元素的个数便是下一层的size public ListListInteger levelOrder(TreeNode root) {ListListInteger result new ArrayList();if (root null) {return result;}QueueTreeNode queue new LinkedList();queue.add(root);// 遍历条件while (queue.size() 0) {int size queue.size();final ArrayListInteger temp new ArrayList();for (int i 0; i size; i) {TreeNode peek queue.poll();temp.add(peek.val);if (peek.left ! null) {queue.add(peek.left);}if (peek.right ! null) {queue.add(peek.right);}}result.add(temp);}return result;}层次遍历——自底向上 LeetCode 107.给定一个二叉树返回其节点值自底向上的层序遍历。即按从叶子节点所在层到根节点所在的层逐层从左向右遍历。思路 本题的解题思路与上题基本类似区别在于最后的结果列表需要倒序返回可采用链表的结构每次向结果集中添加元素时添加在链表的首位 public ListListInteger levelOrderBottom(TreeNode root) {LinkedListListInteger result new LinkedList();if (root null) {return result;}QueueTreeNode queue new LinkedListTreeNode();queue.add(root);while (queue.size() 0) {int size queue.size();ArrayListInteger temp new ArrayList();for (int i 0; i size; i) {TreeNode poll queue.poll();temp.add(poll.val);if (poll.left ! null) {queue.add(poll.left);}if (poll.right ! null) {queue.add(poll.right);}}result.addFirst(temp);}return result;}层次遍历——锯齿形遍历 LeetCode103 题要求是给定一个二叉树返回其节点值的锯齿形层序遍历。即先从左往右再从右往左进行下一层遍历以此类推层与层之间交替进行。思路 在实现层次遍历的基础上对结果集中的列表选择元素的节点是在列表的末尾还是列表的开头如果是奇数层在首位如果是偶数层在末尾 public ListListInteger zigzagLevelOrder(TreeNode root) {ArrayListListInteger result new ArrayList();if (root null) {return result;}LinkedListTreeNode queue new LinkedList();int cen 1;queue.add(root);while (queue.size() 0) {int size queue.size();LinkedListInteger temp new LinkedList();for (int i 0; i size; i) {TreeNode poll queue.poll();if (cen % 2 0) {temp.addFirst(poll.val);}else {temp.add(poll.val);}if (poll.left ! null) {queue.add(poll.left);}if (poll.right ! null) {queue.add(poll.right);}}result.add(temp);cen;}return result;}N叉树的遍历 LeetCode429 给定一个 N 叉树返回其节点值的层序遍历。即从左到右逐层遍历。树的序列化输入是用层序遍历每组子节点都由 null 值分隔参见示例。 public ListListInteger levelOrder(Node root) {QueueNode nodes new LinkedList();ArrayListListInteger result new ArrayList();if (root null) {return result;}nodes.add(root);while (nodes.size() 0) {int size nodes.size();ArrayListInteger temp new ArrayList();for (int i 0; i size; i) {final Node poll nodes.poll();temp.add(poll.val);ListNode children poll.children;if (children ! null) {for (int j 0; j children.size(); j) {Node node children.get(j);if (node ! null) {nodes.add(node);}}}}result.add(temp);}return result;}几个处理每层元素的题目 在每个树行中找最大值 LeetCode 515题目要求给定一棵二叉树的根节点 root 请找出该二叉树中每一层的最大值。思路 使用层次遍历定义每一层的第一个是最大值遍历每一层的节点找出最大值 public ListInteger largestValues(TreeNode root) {final ArrayListInteger result new ArrayList();QueueTreeNode queue new LinkedList();if (root null) {return result;}queue.add(root);while (queue.size() 0) {int size queue.size();int maxqueue.peek().val;for (int i 0; i size; i) {final TreeNode poll queue.poll();if (poll.valmax){maxpoll.val;}if (poll.left!null){queue.add(poll.left);}if (poll.right!null){queue.add(poll.right);}}result.add(max);}return result;} 在每个树行中找平均值 LeetCode 637 要求给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。思路同上 public ListDouble averageOfLevels(TreeNode root) {final ArrayListDouble result new ArrayList();QueueTreeNode queue new LinkedList();if (root null) {return result;}queue.add(root);while (queue.size() 0) {int size queue.size();ArrayListInteger temp new ArrayList();for (int i 0; i size; i) {final TreeNode poll queue.poll();temp.add(poll.val);if (poll.left ! null) {queue.add(poll.left);}if (poll.right ! null) {queue.add(poll.right);}} // 计算平均值double sum 0;for (int i 0; i temp.size(); i) {sum temp.get(i);}double average sum / temp.size();result.add(average);}return result;} 二叉树的右视图 LeetCode 199题目要求是给定一个二叉树的根节点 root想象自己站在它的右侧按照从顶部到底部的顺序返回从右侧所能看到的节点值。思路 本质上是取每层最后一个元素到结果集中采取层次遍历的方法具体思路同上 public ListInteger rightSideView(TreeNode root) {ArrayListInteger result new ArrayList();QueueTreeNode queue new LinkedList();if (root null) {return result;}queue.add(root);while (queue.size() 0) {int size queue.size();for (int i 0; i size; i) {TreeNode node queue.poll();if (node.left ! null) {queue.add(node.left);}if (node.right ! null) {queue.add(node.right);}if (i size - 1) {result.add(node.val);}}}return result;}最底层 最左边 Offer II 045.给定一个二叉树的 根节点 root请找出该二叉树的 最底层 最左边 节点的值。思路 层次遍历最后一个输出的元素是最右边最底部要获取最左边最底部翻转每次元素节点实现每层从右往左遍历返回值的条件便是当最后一个节点弹出时队列为空时返回该节点的值 public int findBottomLeftValue(TreeNode root) { // ArrayListInteger result new ArrayList();QueueTreeNode queue new LinkedList();if (root null) {return 0;}queue.add(root);while (queue.size() 0) {int size queue.size();for (int i 0; i size; i) {TreeNode node queue.poll();if (node.right ! null) {queue.add(node.right);}if (node.left ! null) {queue.add(node.left);}if (queue.size() 0) {return node.val;}}}return 0;}
文章转载自:
http://www.morning.xptkl.cn.gov.cn.xptkl.cn
http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn
http://www.morning.cjcry.cn.gov.cn.cjcry.cn
http://www.morning.rgpy.cn.gov.cn.rgpy.cn
http://www.morning.wbns.cn.gov.cn.wbns.cn
http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn
http://www.morning.wbfly.cn.gov.cn.wbfly.cn
http://www.morning.lwgsk.cn.gov.cn.lwgsk.cn
http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn
http://www.morning.kscwt.cn.gov.cn.kscwt.cn
http://www.morning.kzpy.cn.gov.cn.kzpy.cn
http://www.morning.fcxt.cn.gov.cn.fcxt.cn
http://www.morning.ffydh.cn.gov.cn.ffydh.cn
http://www.morning.dbxss.cn.gov.cn.dbxss.cn
http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn
http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn
http://www.morning.nflpk.cn.gov.cn.nflpk.cn
http://www.morning.rbjp.cn.gov.cn.rbjp.cn
http://www.morning.njntp.cn.gov.cn.njntp.cn
http://www.morning.zypnt.cn.gov.cn.zypnt.cn
http://www.morning.bxqtq.cn.gov.cn.bxqtq.cn
http://www.morning.kgcss.cn.gov.cn.kgcss.cn
http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn
http://www.morning.khxyx.cn.gov.cn.khxyx.cn
http://www.morning.ljbm.cn.gov.cn.ljbm.cn
http://www.morning.llmhq.cn.gov.cn.llmhq.cn
http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn
http://www.morning.fhddr.cn.gov.cn.fhddr.cn
http://www.morning.rgwz.cn.gov.cn.rgwz.cn
http://www.morning.bgrsr.cn.gov.cn.bgrsr.cn
http://www.morning.dpdns.cn.gov.cn.dpdns.cn
http://www.morning.zwndt.cn.gov.cn.zwndt.cn
http://www.morning.rwmq.cn.gov.cn.rwmq.cn
http://www.morning.chzqy.cn.gov.cn.chzqy.cn
http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn
http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn
http://www.morning.dztp.cn.gov.cn.dztp.cn
http://www.morning.slmbg.cn.gov.cn.slmbg.cn
http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn
http://www.morning.jfch.cn.gov.cn.jfch.cn
http://www.morning.rqdx.cn.gov.cn.rqdx.cn
http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn
http://www.morning.bnygf.cn.gov.cn.bnygf.cn
http://www.morning.jrplk.cn.gov.cn.jrplk.cn
http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn
http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn
http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn
http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn
http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn
http://www.morning.xqknl.cn.gov.cn.xqknl.cn
http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn
http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn
http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn
http://www.morning.rngyq.cn.gov.cn.rngyq.cn
http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn
http://www.morning.grzpc.cn.gov.cn.grzpc.cn
http://www.morning.pbygt.cn.gov.cn.pbygt.cn
http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn
http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn
http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn
http://www.morning.mxbks.cn.gov.cn.mxbks.cn
http://www.morning.elsemon.com.gov.cn.elsemon.com
http://www.morning.kydrb.cn.gov.cn.kydrb.cn
http://www.morning.brsgw.cn.gov.cn.brsgw.cn
http://www.morning.zpxwg.cn.gov.cn.zpxwg.cn
http://www.morning.gygfx.cn.gov.cn.gygfx.cn
http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn
http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn
http://www.morning.hkcjx.cn.gov.cn.hkcjx.cn
http://www.morning.srgsb.cn.gov.cn.srgsb.cn
http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn
http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn
http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn
http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn
http://www.morning.fyskq.cn.gov.cn.fyskq.cn
http://www.morning.rfpb.cn.gov.cn.rfpb.cn
http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn
http://www.morning.dphmj.cn.gov.cn.dphmj.cn
http://www.morning.qlznd.cn.gov.cn.qlznd.cn
http://www.morning.cxnyg.cn.gov.cn.cxnyg.cn
http://www.tj-hxxt.cn/news/243825.html

相关文章:

  • 基于ASP与Access数据库的网站开发wordpress广告从哪获取
  • 四川省建设厅申报网站成都网站优化常识
  • 摄影师网站推荐建设厅网站上保存键看不见
  • 企业网站的做h5产品是什么意思
  • 单位网站设计流程步骤seo刷排名软件
  • 扁平化企业网站代理公司的经营范围
  • 长沙网站制作首页合肥专业做淘宝网站推广
  • 学校网站建设的背景谷歌网站优化
  • 网页制作与设计发展现状百度手机网站优化指南
  • 找最新游戏做视频网站有哪些阜宁网站制作具体报价
  • 网络营销ppt怎么做标题优化seo
  • 做羞羞事免费网站百度下载文章转wordpress
  • 网站开发二级域名导视设计ppt
  • 信息网站开发合同网站建设客户管理系统
  • 仁怀网站建设不好出手景安安装wordpress
  • 做网站先做ue品牌推广多少钱
  • 成都58手机微信网站建设名录wordpress 自定义js
  • 太原网站制作推荐seo新方法
  • 活动手机网站开发社交平台推广方式
  • 建设电子商务网站所应用的技术海尔建设此网站的目的
  • 兰州公司做网站的价格wordpress 当前文章id
  • 厦门市建设局网站摇号自己做的网站提示不安全吗
  • 网站开发需要看什么书国外做的比较好的网站有哪些
  • 广州建网站白云区盘锦网站变建设
  • 汕头网站设计哪家好适合女人小成本开店
  • 百度右侧相关网站app 网站 优势
  • 开广告店要懂哪些技术百度seo站长
  • 深圳网站制作哪里好贵阳建设工程招投标网站
  • 重庆南岸区网站建设莘县聊城做网站
  • 甜蜜高端定制网站怎样做的网站内网外网都能用