二叉树前序、中序、后序遍历——Java实现

在非递归实现中,需要借助栈来实现。前序和中序比较类似,也简单一些,但是后序遍历需要两个栈来进行辅助,稍微复杂一些。
在这里插入图片描述
前序遍历:先遍历根结点,然后遍历左子树,最后遍历右子树
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);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值