java achieve binary tree traversal of the depth and breadth

Binary tree node structure:

public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

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

Achievements:

public static void insert(TreeNode root, int val) {// 向二叉树中插入子节点
		if (val > root.val) {// 二叉树的左节点都比根节点小
			if (root.right == null) {
				root.right = new TreeNode(val);
			} else {
				insert(root.right, val);
			}
		} else { // 二叉树的右节点都比根节点大
			if (root.left == null) {
				root.left = new TreeNode(val);
			} else {
				insert(root.left, val);
			}
		}
	}

Recursive preamble:

public static void preOrder(TreeNode root) { // 前序遍历
		if (root != null) {
			System.out.print(root.val + "-");
			preOrder(root.left);
			preOrder(root.right);
		}
	}

The preamble non-recursive implementation:

public static void preStackIterator(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		if (root == null)
			return;
		stack.push(root);
		while (!stack.isEmpty()) {
			TreeNode temp = stack.pop();
			System.out.print(temp.val + " ");
			if (temp.right != null)
				stack.push(temp.right);
			if (temp.left != null)
				stack.push(temp.left);
		}
	}

In order recursive:

public static void inOrder(TreeNode root) { // 前序遍历
		if (root != null) {
			inOrder(root.left);
			System.out.print(root.val + "-");
			inOrder(root.right);
		}
	}

In order to achieve non-recursive:

public static void inStackIterator(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		if (root == null)
			return;
		TreeNode temp = root;
		while (!stack.isEmpty() || temp != null) {
			while (temp != null) {
				stack.push(temp);
				temp = temp.left;
			}
			temp = stack.pop();
			System.out.print(temp.val + " ");
			if (temp.right != null)
				temp = temp.right;
			else
				temp = null;
		}
	}

After the order recursive implementation:

public static void postOrder(TreeNode root) { // 前序遍历
		if (root != null) {
			postOrder(root.left);
			postOrder(root.right);
			System.out.print(root.val + "-");
		}
	}

Subsequent non-recursive implementation:

public static void postStackIterator(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		if (root == null)
			return;
		TreeNode temp = root;
		stack.push(temp);
		TreeNode ptr = root, pre = null;
		while (!stack.isEmpty()) {
			ptr = stack.peek();
			if (pre != ptr.right && pre != ptr.left) {
				if (ptr.right != null)
					stack.push(ptr.right);
				if (ptr.left != null)
					stack.push(ptr.left);
			}
			if (ptr.left == null && ptr.right == null || pre == ptr.left
					|| pre == ptr.right) {
				System.out.print(ptr.val + " ");
				stack.pop();
			}
			pre = ptr;
		}
	}

Breadth-first traversal:

public static void preListIterator(TreeNode root) {
		ArrayList<TreeNode> list = new ArrayList<>();
		if (root == null)
			return;
		list.add(root);
		while (!list.isEmpty()) {
			TreeNode temp = list.remove(0);
			System.out.print(temp.val + " ");
			if (temp.left != null)
				list.add(temp.left);
			if (temp.right != null)
				list.add(temp.right);
		}
	}
Published 21 original articles · won praise 10 · views 8090

Guess you like

Origin blog.csdn.net/weixin_44997483/article/details/103507526