二叉树的遍历 Java实现递归与非递归

package core;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

class TreeNode {
	TreeNode left;
	TreeNode right;
	int value;
	TreeNode(int value) {
		this.value = value;
	}
}

public class TreeNodeTest {

	TreeNode root;
	
	TreeNodeTest() {
		
		/**
		 * 		  1
		 * 		/	\
		 * 	   2     3
		 *    / \   / \
		 *   4   5 6   7
		 *   
		 */
		
		root = new TreeNode(1);
		root.left = new TreeNode(2);
		root.right = new TreeNode(3);
		
		root.left.left = new TreeNode(4);
		root.left.right = new TreeNode(5);
		
		root.right.left = new TreeNode(6);
		root.right.right = new TreeNode(7);
	}
	
	
	//递归遍历 
	
	/**
	 * 前序遍历
	 * 根->左->右
	 * @param root
	 */
	void qian(TreeNode root) {
		if(root == null) {
			return;
		}
		System.out.print(root.value);
		qian(root.left);
		qian(root.right);
	}
	
	/**
	 * 中序遍历
	 * 左->根->右
	 * @param root
	 */
	void zhong(TreeNode root) {
		if(root == null) {
			return;
		}
		zhong(root.left);
		System.out.print(root.value);
		zhong(root.right);
	}
	
	/**
	 * 后序遍历
	 * 左->右->根
	 * @param root
	 */
	void hou(TreeNode root) {
		if(root == null) {
			return;
		}
		hou(root.left);
		hou(root.right);
		System.out.print(root.value);
	}
	
	
	//非递归实现
	
	/**
	 * 前序
	 * @param root
	 */
	void _qian(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		while(root != null || !stack.isEmpty()) {
			while(root != null) {
				System.out.print(root.value);
				stack.push(root);
				root = root.left;
			}
			if(!stack.isEmpty()) {
				root = stack.pop().right;
			}
		}
	}
	/**
	 * 中序
	 * @param root
	 */
	void _zhong(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		while(root != null || !stack.isEmpty()) {
			while(root != null) {
				stack.push(root);
				root = root.left;
			}
			if(!stack.isEmpty()) {
				root = stack.pop();
				System.out.print(root.value);
				root = root.right;
			}
		}
	}
	
	/**
	 * 后序
	 * @param root
	 */
	void _hou(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		Stack<Integer> _stack = new Stack<>();//辅助栈
		int left = 1, right = 2;
		while(root != null || !stack.isEmpty()) {
			
			while(root != null) {
				stack.push(root);
				_stack.push(left);
				root = root.left;
			}
			while(!stack.isEmpty() && _stack.peek() == right) {//重右节点出  完成
				_stack.pop();
				System.out.print(stack.pop().value);
			}
			
			if(!stack.isEmpty() && _stack.peek() == left) {
				_stack.pop();
				_stack.push(right);
				root = stack.peek().right;
			}
		}
	}
	
	
	/**
	 * 层序遍历
	 * @param root
	 */
	void ceng(TreeNode root) {
		if(root == null) return;
		Queue<TreeNode> que = new LinkedList<>();
		que.add(root);
		TreeNode temp;
		while(!que.isEmpty()) {
			temp = que.poll();
			System.out.print(temp.value);
			if(temp.left != null) {
				que.add(temp.left);
			}
			if(temp.right != null) {
				que.add(temp.right);
			}
		}
	}
	
	
	
	public static void main(String[] args) {
		
		TreeNodeTest t = new TreeNodeTest();
		t._hou(t.root);
		System.out.println();
		t.hou(t.root);
		
		System.out.println();
		t.qian(t.root);
		System.out.println();
		t._qian(t.root);
		
		System.out.println();
		t.zhong(t.root);
		System.out.println();
		t._zhong(t.root);
		
		
		System.out.println();
		t.ceng(t.root);
	}
	
}





--------------------------------------------

输出:
4526731
4526731
1245367
1245367
4251637
4251637
1234567





 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值