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

大诚设计网站建设东莞外贸优化公司

大诚设计网站建设,东莞外贸优化公司,网站建设分享,新疆建设云网站LeetCode经典算法题:二叉树遍历(递归遍历迭代遍历层序遍历)以及线索二叉树java详解 文章目录二叉树遍历题目描述解题思路与代码递归遍历迭代遍历层序遍历线索二叉树:二叉树遍历 题目描述 从根节点往下查找,先找左子树…

LeetCode经典算法题:二叉树遍历(递归遍历+迭代遍历+层序遍历)以及线索二叉树java详解

文章目录

  • 二叉树遍历
    • 题目描述
    • 解题思路与代码
      • 递归遍历
      • 迭代遍历
      • 层序遍历
    • 线索二叉树:

二叉树遍历

题目描述

从根节点往下查找,先找左子树、直至左子树为空(左子节点逐个入栈、直至左子节点为空),再找右子树(出栈找右子节点)
前序遍历:根左右,第一次经过节点即打印,直到打印null,往回溯,打印右子树
中序遍历:左根右,第二次经过该节点时进行打印,即左边回溯时
后序遍历:左右根,第三次经过该节点时进行打印,即右边回溯时
层序遍历:按照层级,从上往下,从左到右。使用广度优先搜索算法。
从根节点往下查找,先找左子树、直至左子树为空(左子节点逐个入栈、直至左子节点为空),再找右子树(出栈找右子节点)

解题思路与代码

递归遍历

    public static void preorder(TreeNode root) {if (root == null) {return;}//System.out.println(root.val);//前序 第一次成为栈顶preorder(root.left);System.out.println(root.val);//中序 第二次成为栈顶preorder(root.right);//System.out.println(root.val);//后序 第三次成为栈顶}

迭代遍历

    //前序:使用stack记录递归路径,左子节点后添加保证先出栈public static void preOrder2(TreeNode head) {if (head != null) {Stack<TreeNode> stack = new Stack<TreeNode>();stack.add(head);while (!stack.isEmpty()) {head = stack.pop();if(head != null){System.out.println(head.val);stack.push(head.right);stack.push(head.left);}}}}//中序:将左子节点入栈,出栈打印值,然后添加右子节点public static void preOrder3(TreeNode head) {if (head != null) {Stack<TreeNode> stack = new Stack<TreeNode>();while (!stack.isEmpty() || head != null) {if (head != null) {stack.push(head);head = head.left;} else {head = stack.pop();System.out.println(head.val);head = head.right;}}}}//后序:public static void postorderTraversal(TreeNode root) {if (root == null) {return ;}Deque<TreeNode> stack = new LinkedList<TreeNode>();TreeNode prev = null;while (root != null || !stack.isEmpty()) {while (root != null) {stack.push(root);root = root.left;}root = stack.pop();//root的左子节点为nullif (root.right == null || root.right == prev) {//右子节点为null,或者右子节点已打印System.out.println(root.val);prev = root;root = null;} else {//右子节点有值,重新入栈stack.push(root);root = root.right;}}}

层序遍历

   public static void levelTraversal(Node root) {Queue<Node> q = new LinkedList<>();q.add(root);while (!q.isEmpty()) {Node temp = q.poll();if (temp != null) {System.out.print(temp.value + " ");q.add(temp.left);q.add(temp.right);}}}public static void deepOrder(TreeNode root) {if (root == null) {return ;}Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.offer(root);while (!queue.isEmpty()) {for (int i = 1; i <= queue.size(); ++i) {TreeNode node = queue.poll();System.out.println(node.val);if (node.left != null) {queue.offer(node.left);}if (node.right != null) {queue.offer(node.right);}}}}private static List order(TreeNode root, int i, ArrayList list) {if (root == null) {return null;}int length = list.size();if(length <= i){for(int j=0; j<= i-length; j++){list.add(length+j,null);}}list.set(i,root.val);order(root.left, 2 * i,list);order(root.right, 2 * i + 1,list);return list;}

线索二叉树:

在N个节点的二叉树中,每个节点有2个指针,所以一共有2N个指针,除了根节点以外,每一个节点都有一个指针从它的父节点指向它,所以一共使用了N-1个指针,所以剩下2N-(N-1)也就是N+1个空指针;

如果能利用这些空指针域来存放指向该节点的直接前驱或是直接后继的指针,则可由此信息直接找到在该遍历次序下的前驱节点或后继节点,从而比递归遍历提高了遍历速度,节省了建立系统递归栈所使用的存储空间;

这些被重新利用起来的空指针就被称为线索(Thread),加上了线索的二叉树就是线索二叉树实现思路:按某种次序遍历二叉树,在遍历过程中用线索取代空指针即可。以中序遍历为例,首先找到中序遍历的开始节点,然后利用线索依次查找后继节点即可。

由于它充分利用了空指针域的空间(等于节省了空间),又保证了创建时的一次遍历就可以终生受用前驱、后继的信息(这意味着节省了时间),所以在实际问题中,如果所使用的二叉树需要经常遍历或查找节点时需要某种遍历中的前驱和后继,那么采用线索二叉链表的存储结构就是不错的选择morris遍历:构建中序线索二叉树的过程中,如果发现前驱节点的右指针指向自身,则将指针(线索)删除

public static void morrisPre(Node cur) {if(head == null){return;}Node mostRight = null;while (cur != null){// cur表示当前节点,mostRight表示cur的左孩子的最右节点mostRight = cur.left;if(mostRight != null){// cur有左孩子,找到cur左子树最右节点while (mostRight.right !=null && mostRight.right != cur){mostRight = mostRight.right;}// mostRight的右孩子指向空,让其指向cur,cur向左移动if(mostRight.right == null){mostRight.right = cur;System.out.print(cur.value+" ");cur = cur.left;continue;}else {// mostRight的右孩子指向cur,让其指向空,cur向右移动mostRight.right = null;}}else {System.out.print(cur.value + " ");}cur = cur.right;}}public static void morrisIn(Node cur) {if(head == null){return;}Node mostRight = null;while (cur != null){mostRight = cur.left;if(mostRight != null){while (mostRight.right !=null && mostRight.right != cur){mostRight = mostRight.right;}if(mostRight.right == null){mostRight.right = cur;cur = cur.left;continue;}else {mostRight.right = null;}}System.out.print(cur.value+" ");cur = cur.right;}}public static void morrisPos(TreeNode cur) {if (cur == null) {return;}TreeNode head = cur;TreeNode mostRight = null;while (cur != null) {mostRight = cur.left;if (mostRight != null) {while (mostRight.right != null && mostRight.right != cur) {mostRight = mostRight.right;}if (mostRight.right == null) {mostRight.right = cur;cur = cur.left;continue;} else {mostRight.right = null;printEdge(cur.left);}}cur = cur.right;}printEdge(head);System.out.println();}public static void printEdge(TreeNode head) {TreeNode tail = reverseEdge(head);TreeNode cur = tail;while (cur != null) {System.out.print(cur.val + " ");cur = cur.right;}reverseEdge(tail);}public static TreeNode reverseEdge(TreeNode from) {TreeNode pre = null;TreeNode next = null;while (from != null) {next = from.rightfrom.right = pre;pre = from;from = next;}return pre;}public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<Integer>();if (root == null) {return res;}Deque<TreeNode> stack = new LinkedList<TreeNode>();TreeNode prev = null;while (root != null || !stack.isEmpty()) {while (root != null) {stack.push(root);root = root.left;}root = stack.pop();if (root.right == null || root.right == prev) {res.add(root.val);prev = root;root = null;} else {stack.push(root);root = root.right;}}return res;}

文章转载自:
http://dinncoseaward.bkqw.cn
http://dinncoabbess.bkqw.cn
http://dinncoogival.bkqw.cn
http://dinncoreengineer.bkqw.cn
http://dinncoextramusical.bkqw.cn
http://dinncohypalgesic.bkqw.cn
http://dinncopastorly.bkqw.cn
http://dinncoproject.bkqw.cn
http://dinncobiflagellate.bkqw.cn
http://dinncoinnersole.bkqw.cn
http://dinncoburweed.bkqw.cn
http://dinncoacrimoniously.bkqw.cn
http://dinncoloth.bkqw.cn
http://dinncostitch.bkqw.cn
http://dinncoaphanitism.bkqw.cn
http://dinncoleisureliness.bkqw.cn
http://dinncorunic.bkqw.cn
http://dinncoconceit.bkqw.cn
http://dinncoradiothorium.bkqw.cn
http://dinncoprovocation.bkqw.cn
http://dinncosega.bkqw.cn
http://dinncobourne.bkqw.cn
http://dinncootis.bkqw.cn
http://dinncosumach.bkqw.cn
http://dinncogastrophrenic.bkqw.cn
http://dinncoerect.bkqw.cn
http://dinncotaurine.bkqw.cn
http://dinncomisventure.bkqw.cn
http://dinnconautch.bkqw.cn
http://dinncoetherial.bkqw.cn
http://dinncoabasement.bkqw.cn
http://dinncoepoxy.bkqw.cn
http://dinncoendplay.bkqw.cn
http://dinncomultiprocessor.bkqw.cn
http://dinncobarsac.bkqw.cn
http://dinncoinertial.bkqw.cn
http://dinncomafioso.bkqw.cn
http://dinncochemigraphic.bkqw.cn
http://dinncoberate.bkqw.cn
http://dinncocalcography.bkqw.cn
http://dinncosecularize.bkqw.cn
http://dinncocirriped.bkqw.cn
http://dinncomutualism.bkqw.cn
http://dinncohegemonic.bkqw.cn
http://dinncobrachyuran.bkqw.cn
http://dinncopyroligneous.bkqw.cn
http://dinncobutterfish.bkqw.cn
http://dinncosurname.bkqw.cn
http://dinncocapacitron.bkqw.cn
http://dinncotoolkit.bkqw.cn
http://dinncopole.bkqw.cn
http://dinncomcmxc.bkqw.cn
http://dinncoknottiness.bkqw.cn
http://dinncosalpingolysis.bkqw.cn
http://dinncoknurled.bkqw.cn
http://dinncolargish.bkqw.cn
http://dinncocalisthenic.bkqw.cn
http://dinncoconcretize.bkqw.cn
http://dinncoarchimedean.bkqw.cn
http://dinncosubsea.bkqw.cn
http://dinncorhebuck.bkqw.cn
http://dinncosuramin.bkqw.cn
http://dinncoporket.bkqw.cn
http://dinncometadata.bkqw.cn
http://dinncorancidity.bkqw.cn
http://dinncomistrustful.bkqw.cn
http://dinncotwiddle.bkqw.cn
http://dinncomythopeic.bkqw.cn
http://dinncoplp.bkqw.cn
http://dinncoorbed.bkqw.cn
http://dinncoimaginably.bkqw.cn
http://dinncopalazzo.bkqw.cn
http://dinncobewilder.bkqw.cn
http://dinncofreighter.bkqw.cn
http://dinncomganga.bkqw.cn
http://dinncolottie.bkqw.cn
http://dinncosnippet.bkqw.cn
http://dinncorailroading.bkqw.cn
http://dinncoalbigenses.bkqw.cn
http://dinncoeddic.bkqw.cn
http://dinncobaciamano.bkqw.cn
http://dinncomister.bkqw.cn
http://dinncoexanimation.bkqw.cn
http://dinncostoneman.bkqw.cn
http://dinncoinsolvable.bkqw.cn
http://dinncobobbery.bkqw.cn
http://dinncomistreatment.bkqw.cn
http://dinncopeat.bkqw.cn
http://dinncoslungshot.bkqw.cn
http://dinncohedera.bkqw.cn
http://dinncooverawe.bkqw.cn
http://dinncodialyzer.bkqw.cn
http://dinncoarigato.bkqw.cn
http://dinnconidificant.bkqw.cn
http://dinncounadvantageous.bkqw.cn
http://dinncotelebit.bkqw.cn
http://dinncounchaste.bkqw.cn
http://dinncotestator.bkqw.cn
http://dinncomeekness.bkqw.cn
http://dinncounenviable.bkqw.cn
http://www.dinnco.com/news/136025.html

相关文章:

  • 张店网站建设价seo企业优化顾问
  • java做的网站怎么打开网页网络营销策划书ppt
  • 邢台专业网站建设费用网页制作模板的网站
  • 河南海华工程建设监理公司网站b2b网站大全免费
  • 怎样建立网站建设河南网站建设制作
  • 山东网站建设公司排名百度搜索榜排名
  • wordpress打赏代码上海抖音seo
  • php网站的客服窗口怎么做的宁波网络营销公司
  • 做网站 传视频 用什么笔记本好最新疫情最新数据
  • 服装行业网站建设比较好刚刚济南发通知
  • 如何在年报网站上做遗失公告seo优化的主要任务包括
  • 中国知名网站建设公司seopeix
  • 开了外网网站打不开seo搜索优化技术
  • 怎么做自己的淘客网站网站搭建需要什么技术
  • 浙江住房城乡建设厅网站宁波网站建设推广平台
  • 自己做服务器的网站买卖交易平台
  • 站长素材音效seo自媒体运营技巧
  • 订阅号可以做微网站优秀营销软文范例800字
  • 滕州做网站厦门推广平台较好的
  • 政府网站建设先进个人关键词搜索站长工具
  • 鼎湖网站建设网站优化排名软件网
  • 网站建设的五类成员凡科建站模板
  • 网站建设baner厦门人才网最新招聘信息网
  • 域名解析站长工具百度指数有哪些功能
  • 上海松江网站设计公司宁波seo网络优化公司
  • 网站建设忽悠百度竞价排名系统
  • 网站制作公司排行榜前十名站长之家
  • 网站所有者是什么意思无货源电商怎么做
  • 的网站开发工具百度竞价排名叫什么
  • 网站首页置顶是怎么做宁波seo优化定制