在非递归实现中,需要借助栈来实现。前序和中序比较类似,也简单一些,但是后序遍历需要两个栈来进行辅助,稍微复杂一些。
前序遍历:先遍历根结点,然后遍历左子树,最后遍历右子树
4 2 1 3 6 5 7 8
中序遍历:先遍历左子树,然后遍历根结点,最后遍历右子树
1 2 3 4 5 6 7 8
后序遍历:先遍历左子树,然后遍历右子树,最后遍历根结点
1 3 2 5 8 7 6 4
1、递归实现
class TreeNode{
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public class OrderTree {
//前序遍历(递归实现)
public static void preOrderRecur(TreeNode node) {
if(node == null) {
return;
}
System.out.print(node.val + ",");
preOrderRecur(node.left);
preOrderRecur(node.right);
}
//中序遍历(递归实现)
public static void inOrderRecur(TreeNode node) {
if(node == null) {
return;
}
inOrderRecur(node.left);
System.out.print(node.val + ",");
inOrderRecur(node.right);
}
//后序遍历(递归实现)
public static void posOrderRecur(TreeNode node) {
if(node == null) {
return;
}
posOrderRecur(node.left);
posOrderRecur(node.right);
System.out.print(node.val + ",");
}
public static void main(String args[]) {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
root.right = new TreeNode(6);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(7);
root.right.right.right = new TreeNode(8);
System.out.println("前序遍历递归实现:");
preOrderRecur(root);
System.out.println();
System.out.println("中序遍历递归实现:");
inOrderRecur(root);
System.out.println();
System.out.println("后序遍历递归实现:");
posOrderRecur(root);
}
}
2、非递归实现
import java.util.Stack;
class TreeNode{
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public class OrderTree {
//前序遍历(非递归实现)
public static void preOrderUnRecur(TreeNode node) {
if(node == null) {
return;
}
Stack<TreeNode> s = new Stack<>();
s.push(node);
while(! s.isEmpty()) {
TreeNode temp = s.pop();
System.out.print(temp.val + ",");
if(temp.right != null) {
s.push(temp.right);
}
if(temp.left != null) {
s.push(temp.left);
}
}
}
//中序遍历(非递归实现)
public static void inOrderUnRecur(TreeNode node) {
if(node == null) {
return;
}
Stack<TreeNode> s = new Stack<>();
while(! s.isEmpty() || node != null) {
if(node != null) {
s.push(node);
node = node.left;
}else {
node = s.pop();
System.out.print(node.val + ",");
node = node.right;
}
}
}
//后序遍历(非递归实现)
public static void posOrderUnRecur(TreeNode node) {
if(node == null) {
return;
}
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
s1.push(node);
while(! s1.isEmpty()) {
node = s1.pop();
s2.push(node);
if(node.left != null) {
s1.push(node.left);
}
if(node.right != null) {
s1.push(node.right);
}
}
while(! s2.isEmpty()) {
System.out.print(s2.pop().val + ",");
}
}
public static void main(String args[]) {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
root.right = new TreeNode(6);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(7);
root.right.right.right = new TreeNode(8);
System.out.println("前序遍历非递归实现:");
preOrderUnRecur(root);
System.out.println();
System.out.println("中序遍历非递归实现:");
inOrderUnRecur(root);
System.out.println();
System.out.println("后序遍历非递归实现:");
posOrderUnRecur(root);
}
}