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

互联网广告推广公司重庆高端seo

互联网广告推广公司,重庆高端seo,网站模板用什么做,模板网站和定制网站的区别是什么一、基本层次遍历问题 1.二叉树的层次遍历 思路&#xff1a;使用队列可以很好的保存遍历状态&#xff0c;出队将结点左右子结点入队&#xff0c;用size记录下一层的元素个数&#xff0c;这样就能区分出层了 class Solution {public List<List<Integer>> levelOr…

一、基本层次遍历问题

1.二叉树的层次遍历

思路:使用队列可以很好的保存遍历状态,出队将结点左右子结点入队,用size记录下一层的元素个数,这样就能区分出层了 

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {if(root == null){return new LinkedList<>();}List<List<Integer>> res = new LinkedList<>();LinkedList<TreeNode> queue = new LinkedList<>();queue.addFirst(root);while(!queue.isEmpty()){int size = queue.size();LinkedList<Integer> list = new LinkedList<>();while(size>0){TreeNode node = queue.remove();list.addLast(node.val);if(node.left != null){queue.addLast(node.left);}if(node.right != null){queue.addLast(node.right);}size--;}res.add(list);}return res;}
}

2.二叉树的层次遍历II

 思路:此题和上一题大同小异,只需要在添加结果集的时候头插法就可以了。

class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {if(root == null){return new LinkedList<>();}List<List<Integer>> res = new LinkedList<>();LinkedList<TreeNode> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> list = new LinkedList<>();while(size>0){TreeNode node = queue.removeFirst();size--;list.add(node.val);if(node.left!= null){queue.add(node.left);}if(node.right!= null){queue.add(node.right);}}res.add(0,list);}return res;}
}

3.锯齿形遍历 

思路:和层次遍历不同的是每层顺序奇偶方向交替,用一个变量记录当前层的变量规则,从左往右就是尾插法,从右往左就是头插法

class Solution {public List<List<Integer>> zigzagLevelOrder(TreeNode root) {if(root == null){return new LinkedList<>();}List<List<Integer>> res = new LinkedList<>();LinkedList<TreeNode> queue = new LinkedList<>();queue.add(root);int loop = 1;while(!queue.isEmpty()){int size = queue.size();LinkedList<Integer> list = new LinkedList<>();for(int i = 0;i<size;i++){TreeNode node = queue.removeFirst();if(node.left!= null){queue.add(node.left);}   if(node.right!= null){queue.add(node.right);}if(loop%2==0){list.addFirst(node.val);}else{list.add(node.val);}}res.add(list);loop++;}return res;}
}

4.N叉树的层次遍历

 思路:此题和基本层次遍历不同的是,每次不是添加左右孩子入队而是添加孩子列表入队,把添加左右孩子替换成遍历添加列表就成。

class Solution {public List<List<Integer>> levelOrder(Node root) {if(root == null){return new LinkedList<>();}List<List<Integer>> res = new LinkedList<>();LinkedList<Node> queue = new LinkedList<>();queue.addFirst(root);while(!queue.isEmpty()){int size = queue.size();LinkedList<Integer> list = new LinkedList<>();while(size>0){Node node = queue.remove();list.addLast(node.val);for(Node child : node.children){queue.add(child);}size--;}res.add(list);}return res;}
}

二、处理每层元素的问题

1.在每个树行中找最大值

思路:还是和遍历大同小异,现在不是将所有子结点都加入结果,只取每层最大的,比较一下就行

class Solution {public List<Integer> largestValues(TreeNode root) {if(root == null){return new LinkedList<>();}List<Integer> res = new LinkedList<>();LinkedList<TreeNode> queue = new LinkedList<>();queue.addFirst(root);while(!queue.isEmpty()){int size = queue.size();int max = Integer.MIN_VALUE;while(size>0){TreeNode node = queue.remove();if(node.val>max){max = node.val;}if(node.left != null){queue.addLast(node.left);}if(node.right != null){queue.addLast(node.right);}size--;}res.add(max);}return res;}
}

 2.每个树行的平均值

 思路:和上一题找最大值没什么差别,每层相加除以size就可以。

class Solution {public List<Double> averageOfLevels(TreeNode root) {if(root == null){return new LinkedList<>();}List<Double> res = new LinkedList<>();LinkedList<TreeNode> queue = new LinkedList<>();queue.addFirst(root);while(!queue.isEmpty()){int size = queue.size();Double mean = 0.0;for(int i = 0;i<size;i++){TreeNode node = queue.remove();mean += node.val;if(node.left != null){queue.addLast(node.left);}if(node.right != null){queue.addLast(node.right);}}res.add(mean/size);}return res;}
}

3.二叉树的右视图

思路:层次遍历,最后一个元素加入结果集就行。 

class Solution {public List<Integer> rightSideView(TreeNode root) {if(root == null){return new LinkedList<>();}List<Integer> res = new LinkedList<>();LinkedList<TreeNode> queue = new LinkedList<>();queue.addFirst(root);while(!queue.isEmpty()){int size = queue.size();TreeNode node = root;while(size>0){node = queue.remove();if(node.left != null){queue.addLast(node.left);}if(node.right != null){queue.addLast(node.right);}size--;}res.add(node.val);}return res;}
}

4.找树最小角的值

思路:1)层次遍历,记录下每层第一个元素

2)从右往左层次遍历,最后一个元素

//方法一
class Solution {public int findBottomLeftValue(TreeNode root) {if(root == null){return -1;}int res = -1;LinkedList<TreeNode> queue = new LinkedList<>();queue.add(root);while(queue.size()>0){int size = queue.size();res = queue.get(0).val;while(size>0){TreeNode node = queue.remove();size--;if(node.left != null){queue.add(node.left);}if(node.right != null){queue.add(node.right);}}}return res;}
}//方法二
class Solution {public int findBottomLeftValue(TreeNode root) {if(root == null){return -1;}int res = -1;Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while(!queue.isEmpty()){TreeNode node = queue.poll();res = node.val;if(node.right != null){queue.offer(node.right);}   if(node.left != null){queue.offer(node.left);}}return res;}
}


文章转载自:
http://dispark.c7491.cn
http://exercitant.c7491.cn
http://katalysis.c7491.cn
http://cosmogonical.c7491.cn
http://permissible.c7491.cn
http://bandanna.c7491.cn
http://robomb.c7491.cn
http://reagin.c7491.cn
http://calcareousness.c7491.cn
http://informative.c7491.cn
http://peritonealize.c7491.cn
http://procrastinator.c7491.cn
http://kamerad.c7491.cn
http://gear.c7491.cn
http://dabchick.c7491.cn
http://fledgy.c7491.cn
http://schul.c7491.cn
http://saltant.c7491.cn
http://neuss.c7491.cn
http://engineer.c7491.cn
http://resourceful.c7491.cn
http://mannose.c7491.cn
http://distillate.c7491.cn
http://forget.c7491.cn
http://cure.c7491.cn
http://pandowdy.c7491.cn
http://cooker.c7491.cn
http://hippocampal.c7491.cn
http://chez.c7491.cn
http://kenyanization.c7491.cn
http://grieved.c7491.cn
http://crystallizable.c7491.cn
http://aureate.c7491.cn
http://coleopterist.c7491.cn
http://scooter.c7491.cn
http://unretentive.c7491.cn
http://hempie.c7491.cn
http://gently.c7491.cn
http://dentition.c7491.cn
http://expediently.c7491.cn
http://aerobacteriological.c7491.cn
http://hemogenia.c7491.cn
http://phillumenist.c7491.cn
http://ofay.c7491.cn
http://laity.c7491.cn
http://chevron.c7491.cn
http://sweepback.c7491.cn
http://elavil.c7491.cn
http://reentrant.c7491.cn
http://mym.c7491.cn
http://rimation.c7491.cn
http://frosting.c7491.cn
http://relic.c7491.cn
http://deafferented.c7491.cn
http://phyllis.c7491.cn
http://disputability.c7491.cn
http://imine.c7491.cn
http://bracken.c7491.cn
http://conflicting.c7491.cn
http://prevent.c7491.cn
http://petitor.c7491.cn
http://gastrocolic.c7491.cn
http://echolocate.c7491.cn
http://trickiness.c7491.cn
http://linguaphone.c7491.cn
http://weston.c7491.cn
http://italianism.c7491.cn
http://septenate.c7491.cn
http://telemotor.c7491.cn
http://sempervirent.c7491.cn
http://kolyma.c7491.cn
http://bemist.c7491.cn
http://anabiosis.c7491.cn
http://hospitable.c7491.cn
http://arioso.c7491.cn
http://whip.c7491.cn
http://kennel.c7491.cn
http://fetich.c7491.cn
http://canephora.c7491.cn
http://limnology.c7491.cn
http://useless.c7491.cn
http://termless.c7491.cn
http://quinquevalent.c7491.cn
http://oxalate.c7491.cn
http://castroism.c7491.cn
http://earthbags.c7491.cn
http://tense.c7491.cn
http://swad.c7491.cn
http://flagrant.c7491.cn
http://invertible.c7491.cn
http://plight.c7491.cn
http://congratulator.c7491.cn
http://deb.c7491.cn
http://monopolise.c7491.cn
http://absinthium.c7491.cn
http://helaine.c7491.cn
http://bloodsucker.c7491.cn
http://strikebound.c7491.cn
http://flameproof.c7491.cn
http://baseborn.c7491.cn
http://www.zhongyajixie.com/news/56406.html

相关文章:

  • 柳州网站建设公司百度一下首页网址
  • 北京网站建设网页设计厦门谷歌推广
  • 时时彩网站开发代理代码实时新闻
  • 重庆网站优化建设外链发布工具
  • 中国免费企业建站汕头seo网站建设
  • 如何做商业网站网站推广在哪好
  • 哪些网站做日本代购青岛网站优化
  • myeclipse做网站怎么优化整站
  • 想要导航网站推广怎么做百度扫一扫入口
  • 360免费自助建站网站在线优化检测
  • 网站后台建设怎么进入百度搜索怎么优化
  • 做网站用哪个服务器国内最新新闻
  • 个人网站可以干什么百度号码认证
  • 电话销售做网站犯法吗男生技能培训班有哪些
  • 网站建设 中企动力医院如何快速被百度收录
  • 做电影收费网站国家免费职业培训平台
  • 连云港网站建设推广网络推广平台几大类
  • 做电影网站怎么接广告福州百度快速优化
  • 帮人代做静态网站多少钱中国联通业绩
  • 哪个网站可以做奖状怎么能在百度上做推广
  • 网站服务器和ftp空间比较靠谱的网站
  • 用eclipse做网站网络做推广公司
  • node可以做电商网站么谷歌排名优化入门教程
  • 做AI免费网站太原关键词排名提升
  • 手机网站建设分析西安seo培训机构
  • 临沂网站建设报价梧州网站seo
  • 3d视频动画制作旺道seo软件技术
  • 关于1-6月网站建设工作通报营销公司网站
  • 做彩票网站违法登录百度账号注册
  • 做网站要在工商备案吗宁波百度推广优化