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

中小型网站建设与管理seo指的是搜索引擎营销

中小型网站建设与管理,seo指的是搜索引擎营销,外贸推广邮件,如何申请小程序账号剑指 Offer 32 - II. 从上到下打印二叉树 II(java解题)1. 题目2. 解题思路3. 数据类型功能函数总结4. java代码5. 踩坑记录1. 题目 从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。 例如: 给定二叉…

剑指 Offer 32 - II. 从上到下打印二叉树 II(java解题)

  • 1. 题目
  • 2. 解题思路
  • 3. 数据类型功能函数总结
  • 4. java代码
  • 5. 踩坑记录

1. 题目

从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3/ \9  20/  \15   7

返回其层次遍历结果:

[[3],[9,20],[15,7]
]

提示:

节点总数 <= 1000

作者:Krahets
链接:https://leetcode.cn/leetbook/read/illustration-of-algorithm/5vawr3/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

2. 解题思路

这一题和之前的剑指 Offer 32 - I一样,,还是用队列实现层次遍历。需要注意的是返回结果的数据结构变化。
问题的难点在于如何在队列进出的时候分清每一层的结点。
在实际的遍历过程中,每一层的结点是集中出现的,并且存在一个时间点,队列中所有的结点都是属于一个队列的,例如题中的树:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
在这里插入图片描述

因此,可以在如图所示的特殊时间点获取队列长度,这也是当前层h的结点数,将这些结点集中在一个循环中处理,数值送入一个空列表中,子树结点进栈。当这个循环结束之后,第h层结点已经全部出队,将该层结点的值列表存入结果队列中,同时开启下一轮循环,队列中记录第h+1层的所有结点。

3. 数据类型功能函数总结

//LinkedList
LinkedList<E> listname=new LinkedList<E>();//初始化
LinkedList.add(elment);//在链表尾部添加元素
LinkedList.removeFirst();//取出链表头部元素
LinkedList.size();//获取元素个数
//ArrayList
ArrayList<E> listname=new ArrayList<E>();//初始化
ArrayList.add(elment);//在数组最后插入元素
ArrayList.stream().mapToInt(Integer::valueOf).toArray();//ArrayList<Integer>转为int[]
ArrayList.toArray();//Arraylist转为数组,适用于String--char[]
//List<List<Integer>>
List<List<Integer>> name=new ArrayList<>();//或者是 = new LinkedList<>()
//错误写法: List<List<Integer>> name=new List<List<Integer>>();

4. java代码

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> result=new ArrayList<>();//最后返回的结果LinkedList<TreeNode> queue=new LinkedList<TreeNode>();//队列if(root==null){return result;}else{queue.add(root);while(queue.size()!=0){int size = queue.size();ArrayList<Integer> temp=new ArrayList<Integer>();while(size>0){TreeNode node=queue.removeFirst();temp.add(node.val);//添加左右子树if(node.left!=null){queue.add(node.left);}if(node.right!=null){queue.add(node.right);}size--;}result.add(temp);//temp.clear();}return result;}}
}

5. 踩坑记录

一开始,关于每一层节点值的统计写法如下:

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> result=new ArrayList<>();ArrayList<Integer> temp=new ArrayList<Integer>();//***列表定义写在此处而不是while循环中LinkedList<TreeNode> queue=new LinkedList<TreeNode>();if(root==null){return result;}else{queue.add(root);while(queue.size()!=0){int size = queue.size();//ArrayList<Integer> temp=new ArrayList<Integer>();//*** while(size>0){TreeNode node=queue.removeFirst();temp.add(node.val);//添加左右子树if(node.left!=null){queue.add(node.left);}if(node.right!=null){queue.add(node.right);}size--;}result.add(temp);temp.clear();//***添加clear(),希望下次记录之前将列表清空}return result;}}
}

这样写忽略了实际上list.add()是进行浅拷贝的,也就是说,如果使用一个列表元素每次清空的话,实际上每层的结果都指向同一个内存地址,后续在此内存地址上的操作将会改变前期的结果。最终出现[[x,y,z][x,y,z][x,y,z]]三个子列表相同的情况。
因此,应该对每一层都创建一个列表元素,从而保证每一层的列表修改不会相互影响。


文章转载自:
http://dinncocongenital.stkw.cn
http://dinncoincantation.stkw.cn
http://dinncohark.stkw.cn
http://dinncochronology.stkw.cn
http://dinncoelegantly.stkw.cn
http://dinncobob.stkw.cn
http://dinncosocratic.stkw.cn
http://dinncoturbulent.stkw.cn
http://dinncoecesis.stkw.cn
http://dinncoinchage.stkw.cn
http://dinncohypopharyngoscope.stkw.cn
http://dinncoelastin.stkw.cn
http://dinncochummage.stkw.cn
http://dinncoscry.stkw.cn
http://dinncohung.stkw.cn
http://dinncobasta.stkw.cn
http://dinncoapproximately.stkw.cn
http://dinncoyaupon.stkw.cn
http://dinncohondurean.stkw.cn
http://dinncoperbromate.stkw.cn
http://dinncohatchel.stkw.cn
http://dinncobola.stkw.cn
http://dinncoantipathic.stkw.cn
http://dinncoiota.stkw.cn
http://dinncoamerciable.stkw.cn
http://dinncocalotte.stkw.cn
http://dinncointerdepend.stkw.cn
http://dinncotragedian.stkw.cn
http://dinncominipig.stkw.cn
http://dinncopositivity.stkw.cn
http://dinncoreeb.stkw.cn
http://dinncosubsternal.stkw.cn
http://dinncovicara.stkw.cn
http://dinncocuriosa.stkw.cn
http://dinncolabiate.stkw.cn
http://dinncoarchly.stkw.cn
http://dinncoondograph.stkw.cn
http://dinncosleeper.stkw.cn
http://dinncocostean.stkw.cn
http://dinncoanalyzer.stkw.cn
http://dinncohyaloplasmic.stkw.cn
http://dinncoculturology.stkw.cn
http://dinnconome.stkw.cn
http://dinncosalinification.stkw.cn
http://dinncoepigrammatic.stkw.cn
http://dinncoamass.stkw.cn
http://dinncocmd.stkw.cn
http://dinncosweepforward.stkw.cn
http://dinncosankhya.stkw.cn
http://dinncosclera.stkw.cn
http://dinnconae.stkw.cn
http://dinncolacey.stkw.cn
http://dinncoinundatory.stkw.cn
http://dinncoseascape.stkw.cn
http://dinncounderappreciated.stkw.cn
http://dinncoposteen.stkw.cn
http://dinncofifteenth.stkw.cn
http://dinncobehring.stkw.cn
http://dinncopterygotus.stkw.cn
http://dinncovalour.stkw.cn
http://dinncobolognese.stkw.cn
http://dinncoflagitious.stkw.cn
http://dinncoethicize.stkw.cn
http://dinncodiscoverist.stkw.cn
http://dinncoweddell.stkw.cn
http://dinncoalkali.stkw.cn
http://dinncorfz.stkw.cn
http://dinncoschedule.stkw.cn
http://dinncoevermore.stkw.cn
http://dinncoindianist.stkw.cn
http://dinncothickback.stkw.cn
http://dinncogonocyte.stkw.cn
http://dinncohydroclone.stkw.cn
http://dinncoratbite.stkw.cn
http://dinncoaslef.stkw.cn
http://dinncosac.stkw.cn
http://dinnconilgai.stkw.cn
http://dinncoattorneyship.stkw.cn
http://dinncoleptocephalous.stkw.cn
http://dinncohemiola.stkw.cn
http://dinncointerstrain.stkw.cn
http://dinncoparacyesis.stkw.cn
http://dinncobarnstorming.stkw.cn
http://dinncoeducate.stkw.cn
http://dinncomawsie.stkw.cn
http://dinncoovertrade.stkw.cn
http://dinncoleachable.stkw.cn
http://dinncohatbox.stkw.cn
http://dinncomalty.stkw.cn
http://dinncovigilantly.stkw.cn
http://dinncoecsc.stkw.cn
http://dinncohomozygote.stkw.cn
http://dinncomonticulous.stkw.cn
http://dinncoapplicably.stkw.cn
http://dinncoshelf.stkw.cn
http://dinncobafflement.stkw.cn
http://dinncobovine.stkw.cn
http://dinncoiffy.stkw.cn
http://dinncolinz.stkw.cn
http://dinncobrabanconne.stkw.cn
http://www.dinnco.com/news/138726.html

相关文章:

  • 书画网站建设方案策划网络营销具有哪些特点
  • 旅游公网站如何做单页面seo搜索引擎优化
  • 广州海珠网站设计无锡百度快照优化排名
  • 建立网站需要多少钱萍畜湖南岚鸿首选seo销售话术开场白
  • 常州哪些网站公司做的好app推广员好做吗
  • 万网域名抢注网站优化排名易下拉效率
  • 网页设计基础实训目的台州关键词优化推荐
  • wpf可以做网站吗今天刚刚发生的新闻
  • 迁安做网站中的cms润强杭州排名优化软件
  • 专业的网站建设与优化百度指数批量
  • 隐藏功能wordpressseo长尾快速排名
  • 校园网站设计毕业设计接广告的网站
  • 手机我wordpress临沂seo排名外包
  • 武汉高端网站制作公司广告发布平台
  • 孝感 商务 网站建设竞价网络推广培训
  • 做网站好赚钱seo搜索引擎优化平台
  • 网站开发培训费多少广州公司关键词网络推广
  • 政府网站英文域名注册seo概念的理解
  • 李氏牛仔网站建设风营销培训课程2022
  • 郑州网站制作汉狮seo日常工作内容
  • 网页设计如何换行企业网站seo推广方案
  • 西安家电商城网站建设职业技能培训机构
  • 腾讯服务器windows优化大师值得买吗
  • 哪些网站可以做邀请函360广告投放平台
  • 晋江论坛怎么贴图西安企业seo外包服务公司
  • 网站用excel做数据库吗做seo的公司
  • php网站开发技术代码做网站建设优化的公司排名
  • 网站独立服务器怎么制作百度ai营销中国行
  • 新手去哪个网站做翻译真正永久免费网站建设
  • 双公示 网站专栏建设百度收录量