二叉树的三种遍历方式(递归+非递归)完整代码

在这里插入图片描述

package sparsearray;

import java.util.Stack;

public class Test {

	public static void main(String[] args) {
		// 创建节点
		Node root = 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);
		Node node8 = new Node(8);
		// 连接节点
		root.left = node2;
		root.right = node3;
		node2.left = node4;
		node2.right = node5;
		node3.left = node6;
		node3.right = node7;
		node4.left = node8;
		//*****************************************
		System.out.print("递归先序遍历:");
		preOrderRecur(root);
		System.out.println();
		System.out.print("非递归先序遍历:");
		preOrderNoRecur(root);
		System.out.println();
		System.out.print("递归中序遍历:");
		inOrderRecur(root);
		System.out.println();
		System.out.print("非递归中序遍历:");
		inOrderNoRecur(root);
		System.out.println();
		System.out.print("递归后序遍历:");
		postOrderRecur(root);
		System.out.println();
		System.out.print("非递归后序遍历:");
		postOrderNoRecur(root);
		System.out.println();
	}

	public static void preOrderRecur(Node root) {
		if (root == null)
			return;
		System.out.print(root.val + "->");
		preOrderRecur(root.left);
		preOrderRecur(root.right);

	}

	public static void inOrderRecur(Node root) {
		if (root == null)
			return;
		inOrderRecur(root.left);
		System.out.print(root.val + "->");
		inOrderRecur(root.right);
	}

	public static void postOrderRecur(Node root) {
		if (root == null)
			return;
		postOrderRecur(root.left);
		postOrderRecur(root.right);
		System.out.print(root.val + "->");
	}

	public static void preOrderNoRecur(Node root) {
		if (root != null) {
			Stack<Node> stack = new Stack<>();
			stack.push(root);
			while (!stack.isEmpty()) {
				Node temp = stack.pop();
				System.out.print(temp.val + "->");
				if (temp.right != null)
					stack.push(temp.right);// 先压入右子节点,根左右,先压进栈的后访问
				if (temp.left != null)
					stack.push(temp.left);
			}
		}

	}

	public static void inOrderNoRecur(Node root) {
		if (root != null) {
			Stack<Node> stack = new Stack<>();
			while (!stack.isEmpty() || root != null) {
				if (root != null) {
					stack.push(root);
					root = root.left;// 遍历左子树,寻找最左边的子节点
				} else {// 左子节点为空,则先输出当前节点,然后开始遍历右子节点
					root = stack.pop();
					System.out.print(root.val + "->");
					root = root.right;

				}
			}
		}

	}

	public static void postOrderNoRecur(Node root) {
		if (root != null) {
			Stack<Node> s1 = new Stack<>();
			Stack<Node> s2 = new Stack<>();
			s1.push(root);
			while (!s1.isEmpty()) {
				root = s1.pop();
				s2.push(root);
				if (root.left != null)// 顺序为根左右 后面还要入另外一个栈 所以这里先压left,后面先访问到的也是left
					s1.push(root.left);
				if (root.right != null)
					s1.push(root.right);

			}
			while (!s2.isEmpty()) {
				System.out.print(s2.pop().val + "->");
			}

		}

	}
}

class Node {
	Node left;
	Node right;
	int val;

	public Node(int val) {
		this.val = val;
	}
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodePanda@GPF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值