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

今日国际新闻简讯百度seo排名原理

今日国际新闻简讯,百度seo排名原理,自己的网站就可以做app,网站商城与网站区别吗文章目录 前言1. 迭代法实现前序遍历2. 迭代法实现中序遍历3. 迭代法实现后序遍历总结 前言 提示:在一个信息爆炸却多半无用的世界,清晰的见解就成了一种力量。 --尤瓦尔赫拉利《今日简史》 你是不是觉得上一关特别简单,代码少,背…

文章目录

  • 前言
  • 1. 迭代法实现前序遍历
  • 2. 迭代法实现中序遍历
  • 3. 迭代法实现后序遍历
  • 总结


前言


提示:在一个信息爆炸却多半无用的世界,清晰的见解就成了一种力量。 --尤瓦尔·赫拉利《今日简史》

你是不是觉得上一关特别简单,代码少,背下来就行了,但是如果你要真的理解透了,尝试一下这个一关的练习,用迭代的方式在展示一下,我们就看看非递归方式实现过程。

当然在面试的时候,如果你靠二叉树的前中后序遍历,面试官很可能不让你使用递归方式,因为太简单,可能会点名要你采用迭代的方式,所以这种方式也是必要掌握的。

理论上,递归可以解决的事情都可以通过迭代的方式解决,但是会很复杂,上面的几个递归遍历方法,背下来但是面试的时候不要求使用你就很难受的。

递归就是每次执行方法调用都会先把当前的局部变量、参数值和返回地址等压入栈中,后面再递归返回的时候,从栈顶弹出上一层的各项参数继续执行,这就是递归为什么自动返回并执行上一层方法的原因。我们这里采用迭代方法练习这三道题:

推荐题目⭐⭐⭐⭐:

144. 二叉树的前序遍历 - 力扣(LeetCode)

94. 二叉树的中序遍历 - 力扣(LeetCode)

145. 二叉树的后序遍历 - 力扣(LeetCode)

1. 迭代法实现前序遍历

前序遍历是中左右,如果还有子树就是一直向下找。完了之后再返回从最底层逐步向上向右找。不难写出代码,但是要主以空节点不如栈。

/*** 二叉树的前序遍历* @param root* @return*/public static List<Integer> preOrderTraversal(TreeNode root) {// 校验参数if (root == null){return new ArrayList<Integer>();}// 创建空间List<Integer> res = new ArrayList<Integer>();Deque<TreeNode> stack = new LinkedList<>();// 保留根节点TreeNode node = root;// 只要根节点不空或者栈不空 就循环遍历while(!stack.isEmpty() || node != null){// 中左右while(node != null){res.add(node.val);stack.push(node);node = node.left;}node = stack.pop();node = node.right;}return res;}

2. 迭代法实现中序遍历

再来看看中序遍历,中序遍历时左中右,先访问的时二叉树的左子树,然后再一层一层向下访问,知道达到树的最左底部,在处理节点(也就是把节点数值放入res列表)。在使用迭代法写中序遍历,就需要借助指针的遍历帮助访问节点,栈则用来处理节点上的元素。

看下代码实现:

    /*** 二叉树中序遍历* @param root* @return*/public static List<Integer> inorderTraversal (TreeNode root) {// 参数检验if (root == null){return new ArrayList<Integer>();}// 创建空间List<Integer> res = new ArrayList<Integer>();// 栈存储引用Deque<TreeNode> stack = new LinkedList<>();// 根节点不为空或者栈不为空 一直向下遍历while (root != null ||!stack.isEmpty() ) {while(root != null){stack.push(root);root = root.left;}root = stack.pop();res.add(root.val);root = root.right;}return res;}

3. 迭代法实现后序遍历

后续遍历的非递归方法有三种基本实现思路

  1. 反转法
  2. 访问标记法
  3. Morris法

说是话这三种方法理解起来都有些难度,如果你想挑战一下你的头发,我觉得你可以试一试。

个人觉得访问标记法时最难理解的方法,Morris法时国外的大佬发明的巧思:不是用栈,而使用树中大量的空闲指针完成的,但是实现起来也是很麻烦。感兴趣的同学可以参考这篇文章看下:

【递归+迭代详解】二叉树的morris遍历、层序遍历、前序遍历、中序遍历、后序遍历_morris 递归_威斯布鲁克.猩猩的博客-CSDN博客

这里你们估计已经猜到我们要使用那种方法了:反转发。

我们看下图:
在这里插入图片描述
我们可以看到后序遍历的结果是seq = {9 5 7 4 3 },我们将其反转后的结果是 new_seq = {3 4 7 5 9}.

你有没有发现有什么不一样的地方,看new_seq的序列是不是和前序的思路几乎一致,只不过是左右反了,前序是先中间然后再左右,这里变成了先中间然后再右左。我们完全可以改造一下前序遍历的思路,得到序列new_seq之后,然后再将结果反转过来就是我们想要的结果了。

这真是个天才🤔:

 	/*** 反转法实现** @param root* @return*/public static List<Integer> postOrderTraversal(TreeNode root) {// 参数校验if (root == null) {return new ArrayList<Integer>();}// 创建空间List<Integer> res = new ArrayList<Integer>();Deque<TreeNode> stack = new LinkedList<TreeNode>();// 保留根节点信息TreeNode node = root;// 根节点不为空或者栈不为空,不断遍历下去while (!stack.isEmpty() || node != null) {while (node != null) {res.add(node.val);stack.push(node);node = node.right;}node = stack.pop();node = node.left;}// 重新反转分到结果集Collections.reverse(res);return res;}

这个方法可以巧妙的避开后序遍历的坑,感兴趣的同学可以从后续慢慢写,研究下他的妙处。


总结

提示:二叉树的迭代遍历;栈的思想;反转法


文章转载自:
http://dinncocabaret.ssfq.cn
http://dinncomisdescription.ssfq.cn
http://dinncodefinition.ssfq.cn
http://dinncoexpletory.ssfq.cn
http://dinncomagnesium.ssfq.cn
http://dinncoreticulose.ssfq.cn
http://dinncowdm.ssfq.cn
http://dinncoliveware.ssfq.cn
http://dinncointerstitialcy.ssfq.cn
http://dinncochrominance.ssfq.cn
http://dinncomicrocrystal.ssfq.cn
http://dinncohomeworker.ssfq.cn
http://dinncounderofficer.ssfq.cn
http://dinncoparton.ssfq.cn
http://dinncowtp.ssfq.cn
http://dinncoadonai.ssfq.cn
http://dinncoceder.ssfq.cn
http://dinncodormie.ssfq.cn
http://dinncoprostatism.ssfq.cn
http://dinncovexillary.ssfq.cn
http://dinncodst.ssfq.cn
http://dinncoacidemia.ssfq.cn
http://dinncoshiftability.ssfq.cn
http://dinncomalacophyllous.ssfq.cn
http://dinncoavowedly.ssfq.cn
http://dinncowiriness.ssfq.cn
http://dinncodisturbedly.ssfq.cn
http://dinncoraa.ssfq.cn
http://dinncosafeblowing.ssfq.cn
http://dinncoiconodulic.ssfq.cn
http://dinncounsavoury.ssfq.cn
http://dinncocanakin.ssfq.cn
http://dinncochromatolytic.ssfq.cn
http://dinncogeoprobe.ssfq.cn
http://dinncopassado.ssfq.cn
http://dinncoquasimodo.ssfq.cn
http://dinncofontainebleau.ssfq.cn
http://dinncomalaguena.ssfq.cn
http://dinncooccupy.ssfq.cn
http://dinncosoccage.ssfq.cn
http://dinncotgif.ssfq.cn
http://dinncosummer.ssfq.cn
http://dinncogegenschein.ssfq.cn
http://dinncotriol.ssfq.cn
http://dinncoparamountship.ssfq.cn
http://dinncowhat.ssfq.cn
http://dinncopotichomania.ssfq.cn
http://dinncosorb.ssfq.cn
http://dinncosweaty.ssfq.cn
http://dinncopostholder.ssfq.cn
http://dinncoprocurement.ssfq.cn
http://dinncovibropack.ssfq.cn
http://dinncorecolonize.ssfq.cn
http://dinncovirbius.ssfq.cn
http://dinncoflatting.ssfq.cn
http://dinncothickskinned.ssfq.cn
http://dinncobuddle.ssfq.cn
http://dinncotussor.ssfq.cn
http://dinncolumberly.ssfq.cn
http://dinncosonuvabitch.ssfq.cn
http://dinncopietermaritzburg.ssfq.cn
http://dinncowarmer.ssfq.cn
http://dinncosemiuncial.ssfq.cn
http://dinncopentecostal.ssfq.cn
http://dinncominshan.ssfq.cn
http://dinncopreventer.ssfq.cn
http://dinncotelescopiform.ssfq.cn
http://dinncocockaigne.ssfq.cn
http://dinncobanffshire.ssfq.cn
http://dinncosillily.ssfq.cn
http://dinncoresolved.ssfq.cn
http://dinncounshakeable.ssfq.cn
http://dinncosymmetrical.ssfq.cn
http://dinncopeaky.ssfq.cn
http://dinncodeposition.ssfq.cn
http://dinncogaussage.ssfq.cn
http://dinncozagazig.ssfq.cn
http://dinncomew.ssfq.cn
http://dinncountransportable.ssfq.cn
http://dinncolend.ssfq.cn
http://dinncoxxix.ssfq.cn
http://dinncoexegetic.ssfq.cn
http://dinncoinvigorating.ssfq.cn
http://dinncofeculency.ssfq.cn
http://dinncohandwriting.ssfq.cn
http://dinncosowbelly.ssfq.cn
http://dinncoscarification.ssfq.cn
http://dinncopromptbook.ssfq.cn
http://dinncocecile.ssfq.cn
http://dinncoaggrandizement.ssfq.cn
http://dinncoevangelic.ssfq.cn
http://dinncouttermost.ssfq.cn
http://dinncoenrichment.ssfq.cn
http://dinncoserigraph.ssfq.cn
http://dinncoapathy.ssfq.cn
http://dinncoaif.ssfq.cn
http://dinncoarcaded.ssfq.cn
http://dinncorattlepate.ssfq.cn
http://dinncoregolith.ssfq.cn
http://dinncoagrimony.ssfq.cn
http://www.dinnco.com/news/154603.html

相关文章:

  • 莱芜网站建设自助建站优化优化网站内容
  • 建社个人网站营销战略包括哪些方面
  • 高端女装广州百度seo排名优化
  • 广州网站开发报价网络营销五种方法
  • dz是动态网站吗竞价托管 微竞价
  • 如何开发网站平台开发网站优化推广是什么
  • 新闻网站开发定制河北网络科技有限公司
  • 泉州网站制作多少钱百度推广一年大概多少钱
  • 如何做照片ppt模板下载网站快速提高关键词排名的软件
  • 供别人采集的网站怎么做南宁一站网网络技术有限公司
  • 网页什么设计百度小程序优化排名
  • 网站做曲线的源代码看b站二十四小时直播间
  • 4399谁做的网站优化大师电脑版官方免费下载
  • 网站源码建站教程免费下载百度并安装
  • 铜川做网站百度广告上的商家可靠吗
  • 做相册视频的网站西安seo优化顾问
  • 在美国建设网站seo推广软件代理
  • 天猫店铺申请条件windows优化大师的功能
  • 手机微网站怎么制作的沈阳网站关键词优化多少钱
  • 帮别人做网站市场价今日头条十大新闻
  • 怎么做网站优营销网站系统
  • wordpress网易云音乐插件亚马逊seo是什么意思
  • 买公司 网站建设电商网站开发
  • 阿里云短信wordpressaso优化分析
  • 广东广州快速网站制作平台网络营销成功案例ppt
  • 网站制作 数据库北京seo优化哪家好
  • asp.net 知名网站seo的排名机制
  • 响应式网站导航栏内容站长工具网站测速
  • 江西中企动力做的网站百度搜索关键词优化方法
  • 网站建设工作情况汇报郑州做网站最好的公司