用非递归方式实现二叉树先序便利
来源:互联网 发布:杭州java平均薪资 编辑:程序博客网 时间:2024/06/04 23:31
先序遍历:中、左、右
中序遍历:左、中、右
后序遍历:左、右、中
比如下面这科树
1
2 34 5 6 7
package com.sangfor.tree; public class Node { public int value; public Node left; public Node right; public Node(int value) { this.value = value; } }
package com.sangfor.tree;import java.util.Stack;public class ForEachTree { public static void main(String[] args) { Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); Node node6 = new Node(6); Node node7 = new Node(7); node1.left = node2; node1.right = node3; node2.left = node4; node2.right = node5; node3.left = node6; node3.right = node7; System.out.println("前序列递归"); priOrder(node1); System.out.println(); System.out.println("前序列非递归"); priUnRecur(node1); System.out.println(); System.out.println("中序列递归"); inOrder(node1); System.out.println(); System.out.println("后序列递归"); posOrder(node1);} //非递归前序 public static void priUnRecur(Node head) { if (head != null) { //这里一定要记得判断head是否为空,编程要严谨 Stack<Node> stack = new Stack<Node>(); stack.push(head); while (!stack.isEmpty()) { head = stack.pop(); System.out.print(head.value + "\t"); if (head.right != null){ stack.push(head.right); } if (head.left != null) { stack.push(head.left); } } } else { System.out.println("tree is no data"); } } //前序列递归 public static void priOrder(Node head) { if (head == null) { return; } System.out.print(head.value+"\t"); priOrder(head.left); priOrder(head.right); } // //非中序列递归// public static void inUncur(Node head) {// if (head != null) {// Stack<Node> stack = new Stack<Node>();// stack.add(head);// while (!stack.isEmpty()) { //注意这个地方不是 stack != null// head = stack.pop();// while (head.left != null) {// head = head.left;// if (head.right != null) {// stack.push(head.right);// }// stack.push(head);// System.out.println(head.value + "\t");// }// //// if (head.right != null) {//// stack.push(head.right);//// //// }// }// } else {// System.out.println("tree is no data");//// if (head.right != null) {//// Node newRight = head.right;//// while (newRight != null) {//// //// }//// }// }// } //中序列递归 public static void inOrder(Node head) { if (head == null) { return; } inOrder(head.left); System.out.print(head.value+"\t"); inOrder(head.right); } //后序列递归 public static void posOrder(Node head) { if (head == null) { return; } posOrder(head.left); posOrder(head.right); System.out.print(head.value+"\t"); }}
结果:
前序列递归1245367前序列非递归1245367中序列递归4251637后序列递归4526731
0 0
- 用非递归方式实现二叉树先序便利
- 用递归和非递归方式实现二叉树
- 非递归遍历便利二叉树
- 用递归和非递归方式实现二叉树先序、中序和后序遍历
- 用非递归方式实现二叉树中序遍历
- 用非递归方式实现二叉树后序遍历
- 分别用递归和非递归方式实现二叉树先序、中序和后序遍历(java实现)
- 二叉树三种遍历方式的递归与非递归实现
- 二叉树三种遍历方式 递归与非递归实现
- 二叉树三种遍历方式 递归与非递归实现 总结
- 二叉树先序遍历非递归实现
- 用非递归方式实现二叉树的前、中、后三种遍历方法
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- 二叉树先序遍历递归和非递归实现
- 二叉树先序遍历 -- 递归和非递归实现
- 分别用递归和非递归方式实现二叉树的先序、中序和后序遍历
- 一个程序理解java二叉树-创建、递归非递归便利、获取路径
- CSS—组合选择器
- HDU 1711 Number Sequence
- 如果获得页面跳转的最终URL
- 很久前Oracle的一些笔记_7-8
- 年终奖(动态规划)---- 京东2016研发工程师编程题
- 用非递归方式实现二叉树先序便利
- 五大常用算法之一:分治算法(转)
- At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger fo
- 【杭电oj2647】Reward
- Leetcode之路 258. Add Digits
- SiteMesh
- eclipse下maven项目开发编译
- 向量vector理解
- HDU 1686 Oulipo