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

在这里插入图片描述

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;
	}
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43478694/article/details/115406473