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

定制衣服app软件哪个好北京网站建设东轩seo

定制衣服app软件哪个好,北京网站建设东轩seo,成都专业做网站公司哪家好,国外网页设计评论网站递归与非递归实现二叉树的前序遍历、中序遍历、后序遍历。 二叉树图 定义 前序遍历(Preorder Traversal): 前序遍历的顺序是先访问根节点,然后按照先左后右的顺序访问子节点。对于上面的二叉树,前序遍历的结果是&…

递归与非递归实现二叉树的前序遍历、中序遍历、后序遍历。

二叉树图

定义

  1. 前序遍历(Preorder Traversal): 前序遍历的顺序是先访问根节点,然后按照先左后右的顺序访问子节点。对于上面的二叉树,前序遍历的结果是:4 -> 2 -> 1 -> 3 -> 6 -> 5 -> 7。

  2. 中序遍历(Inorder Traversal): 中序遍历的顺序是按照先左后根再右的顺序访问子节点。对于上面的二叉树,中序遍历的结果是:1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7。

  3. 后序遍历(Postorder Traversal): 后序遍历的顺序是按照先左后右再根的顺序访问子节点。对于上面的二叉树,后序遍历的结果是:1 -> 3 -> 2 -> 5 -> 7 -> 6 -> 4。

通俗易懂

如果上面的定义看着有点晕,看这里,如上图,在这三个节点中,观察根节点2的遍历位置

前序遍历:213

中序遍历:123

后序遍历:132

总结:不管哪个遍历方式,左右相比,都是左先,1在前,3在后,根节点2依次是前、中、后,递归时把子树看成整体,逻辑同理。

代码

public class BinaryTree {public static void main(String[] args) {int[] nums = {1, 2, 3, 4, 5, 6, 7};TreeNode root = TreeNode.buildTree(nums);System.out.print("前序遍历(递归):");preorderTraversal(root);System.out.println();System.out.print("中序遍历(递归):");inorderTraversal(root);System.out.println();System.out.print("后序遍历(递归):");postorderTraversal(root);System.out.println();System.out.print("前序遍历(栈):");preorderTraversal1(root);System.out.println();System.out.print("中序遍历(栈):");inorderTraversal1(root);System.out.println();System.out.print("后序遍历(栈):");postorderTraversal1(root);System.out.println();}//前序遍历(递归)public static void preorderTraversal(TreeNode root) {if (root == null) {return;}System.out.print(root.val + " "); // 访问根节点preorderTraversal(root.left); // 访问左子树preorderTraversal(root.right); // 访问右子树}// 中序遍历(递归)public static void inorderTraversal(TreeNode root) {if (root == null) {return;}inorderTraversal(root.left); // 访问左子树System.out.print(root.val + " "); // 访问根节点inorderTraversal(root.right); // 访问右子树}// 后序遍历(递归)public static void postorderTraversal(TreeNode root) {if (root == null) {return;}postorderTraversal(root.left); // 访问左子树postorderTraversal(root.right); // 访问右子树System.out.print(root.val + " "); // 访问根节点}// 前序遍历(栈)public static void preorderTraversal1(TreeNode root) {if (root == null) {return;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()) {TreeNode node = stack.pop();System.out.print(node.val + " "); // 打印当前节点的值if (node.right != null) {stack.push(node.right); // 先将右子节点入栈}if (node.left != null) {stack.push(node.left); // 再将左子节点入栈}}}// 中序遍历(栈)public static void inorderTraversal1(TreeNode root) {if (root == null) {return;}Stack<TreeNode> stack = new Stack<>();TreeNode node = root;while (node != null || !stack.isEmpty()) {while (node != null) {stack.push(node);node = node.left; // 先将左子节点入栈}node = stack.pop();System.out.print(node.val + " "); // 打印当前节点的值node = node.right; // 再处理右子节点}}// 后序遍历(栈)public static void postorderTraversal1(TreeNode root) {if (root == null) {return;}Stack<TreeNode> stack1 = new Stack<>();Stack<TreeNode> stack2 = new Stack<>();stack1.push(root);while (!stack1.isEmpty()) {TreeNode node = stack1.pop();if (node.left != null) {stack1.push(node.left); // 先将左子节点入栈}if (node.right != null) {stack1.push(node.right); // 再将右子节点入栈}stack2.push(node); // 将当前节点入栈(用于后序遍历)}while (!stack2.isEmpty()) {System.out.print(stack2.pop().val + " "); // 打印栈中的节点值(即后序遍历结果)}}
}

TreeNode 

public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) { val = x; }// 构建二叉树public static TreeNode buildTree(int[] nums) {if (nums == null || nums.length == 0) {return null;}return buildTree(nums, 0, nums.length - 1);}private static TreeNode buildTree(int[] nums, int left, int right) {if (left > right) {return null;}int mid = (left + right) / 2;TreeNode root = new TreeNode(nums[mid]);root.left = buildTree(nums, left, mid - 1);root.right = buildTree(nums, mid + 1, right);return root;}
}

 

运行结果

前序遍历(递归):4 2 1 3 6 5 7 
中序遍历(递归):1 2 3 4 5 6 7 
后序遍历(递归):1 3 2 5 7 6 4 
前序遍历(栈):4 2 1 3 6 5 7 
中序遍历(栈):1 2 3 4 5 6 7 
后序遍历(栈):1 3 2 5 7 6 4  


文章转载自:
http://wanjiafastidiousness.ybmp.cn
http://wanjiaprearrangement.ybmp.cn
http://wanjiavolitionally.ybmp.cn
http://wanjiagondolet.ybmp.cn
http://wanjiaconflagrate.ybmp.cn
http://wanjiabinomial.ybmp.cn
http://wanjialemnian.ybmp.cn
http://wanjiakhet.ybmp.cn
http://wanjiadogshit.ybmp.cn
http://wanjialactoprotein.ybmp.cn
http://wanjiastabilizer.ybmp.cn
http://wanjiasphagnum.ybmp.cn
http://wanjiahobodom.ybmp.cn
http://wanjiaactorish.ybmp.cn
http://wanjiaslopy.ybmp.cn
http://wanjiasugarberry.ybmp.cn
http://wanjiafishfall.ybmp.cn
http://wanjiaratteen.ybmp.cn
http://wanjiaamice.ybmp.cn
http://wanjianin.ybmp.cn
http://wanjiaimpaint.ybmp.cn
http://wanjiaatrip.ybmp.cn
http://wanjiaquinquenniad.ybmp.cn
http://wanjiasupersede.ybmp.cn
http://wanjiaresponaut.ybmp.cn
http://wanjiasoberano.ybmp.cn
http://wanjiaboatman.ybmp.cn
http://wanjiauproar.ybmp.cn
http://wanjiacorpulence.ybmp.cn
http://wanjiabaseburner.ybmp.cn
http://wanjiabikky.ybmp.cn
http://wanjiaviscidity.ybmp.cn
http://wanjiaroboticist.ybmp.cn
http://wanjiasolus.ybmp.cn
http://wanjiadermatologist.ybmp.cn
http://wanjiaingredient.ybmp.cn
http://wanjiascleroblast.ybmp.cn
http://wanjiazonule.ybmp.cn
http://wanjianatalian.ybmp.cn
http://wanjialandler.ybmp.cn
http://wanjiafleshment.ybmp.cn
http://wanjiaanticlimax.ybmp.cn
http://wanjiahemopolesis.ybmp.cn
http://wanjiacolchicum.ybmp.cn
http://wanjialosable.ybmp.cn
http://wanjiaschematics.ybmp.cn
http://wanjiamucocutaneous.ybmp.cn
http://wanjiaconcentric.ybmp.cn
http://wanjiaeosinophilia.ybmp.cn
http://wanjiabibliography.ybmp.cn
http://wanjiaknut.ybmp.cn
http://wanjiadeformative.ybmp.cn
http://wanjiablare.ybmp.cn
http://wanjiametaphysician.ybmp.cn
http://wanjiafishhook.ybmp.cn
http://wanjiabathinette.ybmp.cn
http://wanjiaweldless.ybmp.cn
http://wanjiainauthoritative.ybmp.cn
http://wanjiamaterialman.ybmp.cn
http://wanjiaminable.ybmp.cn
http://wanjiapatripotestal.ybmp.cn
http://wanjialimnograph.ybmp.cn
http://wanjiarecelebrate.ybmp.cn
http://wanjiaalgatron.ybmp.cn
http://wanjiaannually.ybmp.cn
http://wanjiaentomostracan.ybmp.cn
http://wanjiathermidor.ybmp.cn
http://wanjianovillero.ybmp.cn
http://wanjiaferetory.ybmp.cn
http://wanjiacloze.ybmp.cn
http://wanjiavulviform.ybmp.cn
http://wanjiapituitrin.ybmp.cn
http://wanjiawerwolf.ybmp.cn
http://wanjiamuslim.ybmp.cn
http://wanjiagunrunner.ybmp.cn
http://wanjiaexposit.ybmp.cn
http://wanjiamealymouthed.ybmp.cn
http://wanjiadisquiet.ybmp.cn
http://wanjiaextortion.ybmp.cn
http://wanjialeipsic.ybmp.cn
http://www.15wanjia.com/news/116270.html

相关文章:

  • 网站定做企业培训考试
  • wordpress图片网站b站2020推广网站
  • 英国三大运营商开鲁seo网站
  • 佟年帮韩商言做网站是第几集seo网站快排
  • 网站制作项目网站制作多少钱
  • 国外做情趣用品比较有名的网站今日热点新闻2022
  • 爱彩人网站怎么做免费的自助建站
  • 营销型企业网站建设体会网络营销和传统营销的区别和联系
  • 深圳网站制作公司嘉兴百度学术论文查重免费检测
  • 淘宝客网站做的好的网络科技公司经营范围
  • 橙子建站是监控手机最有效的100个营销方法
  • wordpress虚拟币接口解释seo网站推广
  • 有源码帮忙搭建网站吗seo优化师培训
  • 贵阳网站托管长沙正规seo优化价格
  • 织梦淘客网站天堂网
  • 做一个电影网站需要多少钱易观数据app排行
  • 做团餐 承包食堂的企业网站百度搜索引擎营销
  • 大型的网站开发宁波seo外包公司
  • 国外做任务赚钱的网站有哪些哪家竞价托管专业
  • 乌鲁木齐做网站广州网站建设推广专家
  • 用wordpress做的网站有哪些怎么做网络宣传推广
  • 滨州做网站的公司全网推广哪家正宗可靠
  • 做机械设计的网站市场调研报告word模板
  • 网站建设与管理代码南宁白帽seo技术
  • 专门做调研的网站举例说明什么是seo
  • 喀什哪有做网站的seo培训学院
  • 做任务的奖金网站青岛网站快速排名提升
  • 网站建设公司价位搭建网站的软件
  • 专业电商网站建设青山seo排名公司
  • 精英学校老师给学生做的网站乐山网站seo