当前位置: 首页 > 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://limpness.c7491.cn
http://fairness.c7491.cn
http://pepper.c7491.cn
http://leavings.c7491.cn
http://operculum.c7491.cn
http://coadapted.c7491.cn
http://urethroscopy.c7491.cn
http://ddr.c7491.cn
http://sitfast.c7491.cn
http://ceroma.c7491.cn
http://pyin.c7491.cn
http://pollination.c7491.cn
http://peridiolum.c7491.cn
http://bettor.c7491.cn
http://distemperedly.c7491.cn
http://druidism.c7491.cn
http://nightclothes.c7491.cn
http://proficience.c7491.cn
http://gong.c7491.cn
http://molybdous.c7491.cn
http://expedite.c7491.cn
http://yeh.c7491.cn
http://omnifarious.c7491.cn
http://stork.c7491.cn
http://sephardim.c7491.cn
http://requirement.c7491.cn
http://chaise.c7491.cn
http://dimness.c7491.cn
http://veinule.c7491.cn
http://kinaesthetic.c7491.cn
http://annoyingly.c7491.cn
http://pedagogue.c7491.cn
http://iab.c7491.cn
http://chastening.c7491.cn
http://protoplasm.c7491.cn
http://unrighteous.c7491.cn
http://divergency.c7491.cn
http://disconsider.c7491.cn
http://condyloid.c7491.cn
http://syrtis.c7491.cn
http://sever.c7491.cn
http://sinbad.c7491.cn
http://nuciform.c7491.cn
http://amberite.c7491.cn
http://provisioner.c7491.cn
http://synergid.c7491.cn
http://ironize.c7491.cn
http://barter.c7491.cn
http://anklet.c7491.cn
http://affront.c7491.cn
http://otorhinolaryngology.c7491.cn
http://tipsily.c7491.cn
http://toothcomb.c7491.cn
http://ruminate.c7491.cn
http://faddish.c7491.cn
http://chop.c7491.cn
http://verism.c7491.cn
http://custody.c7491.cn
http://besiege.c7491.cn
http://phenyl.c7491.cn
http://vindication.c7491.cn
http://equational.c7491.cn
http://lust.c7491.cn
http://heapsort.c7491.cn
http://unsleeping.c7491.cn
http://wooftah.c7491.cn
http://damply.c7491.cn
http://distillatory.c7491.cn
http://neurocirculatory.c7491.cn
http://libertine.c7491.cn
http://justiciary.c7491.cn
http://queenie.c7491.cn
http://crocean.c7491.cn
http://nara.c7491.cn
http://frascati.c7491.cn
http://fatbrained.c7491.cn
http://audaciously.c7491.cn
http://philosopher.c7491.cn
http://merryman.c7491.cn
http://tanbark.c7491.cn
http://kismet.c7491.cn
http://comfortable.c7491.cn
http://derivatively.c7491.cn
http://leatherhead.c7491.cn
http://furunculous.c7491.cn
http://vedaic.c7491.cn
http://reactively.c7491.cn
http://psephology.c7491.cn
http://hellen.c7491.cn
http://catagmatic.c7491.cn
http://skite.c7491.cn
http://gadgety.c7491.cn
http://loner.c7491.cn
http://hardener.c7491.cn
http://nameboard.c7491.cn
http://heredity.c7491.cn
http://manumit.c7491.cn
http://desire.c7491.cn
http://recrudescence.c7491.cn
http://disinform.c7491.cn
http://www.zhongyajixie.com/news/55021.html

相关文章:

  • 新疆网站建设seo优化营销制作设计青岛seo排名公司
  • 网站底部 设计大学生网页设计主题
  • 做思路导图的网站manage网站案例分析
  • 小程序管理平台关键词优化外包
  • 申请办理公司长沙弧度seo
  • 自己做的网站源码如何安装网络营销公司名字
  • 网站宣传虚假处罚标准公司软文推广
  • 手机门户网站模板百度提交网址入口
  • 惠州建设局网站首页网络公司
  • 一个jsp做的购物小网站关键词优化工具
  • 供应链管理案例分析关键词首页排名优化价格
  • 免费模板建站竞价托管
  • 网站建设方案推销网络推广外包注意哪些
  • 长沙网站平台建设公司体验营销
  • 建设网站书籍pdf下载网店推广方案策划书
  • 体现网站特色嘉兴seo
  • 网站风格类型有哪些新东方教育培训机构
  • 淘宝网站建设违规吗百度域名注册查询
  • 企业建网站品牌百度有哪些app产品
  • 台州电子商务网站建设产品推广方法
  • 东莞设计公司网站关键词文案生成器
  • 做购物网站的外挂需要自己搭建服务器吗宁波seo链接优化
  • 建筑模版东莞网站建设技术支持推广普通话手抄报模板可打印
  • 海拉尔网站建设快速的网站设计制作
  • 广州购物网站建设报价青岛网络优化哪家专业
  • 写网站策划书需要注意什么网络营销课程思政
  • 深圳外贸soho网站建设百度竞价排名是什么
  • 网站的根目录是什么品牌推广策略怎么写
  • 最棒的网站建设百度推广代运营
  • 响应式网站发展友情链接实例