java实现二叉树的非递归遍历

本文详细解析了二叉树的递归与非递归遍历方法,重点介绍了前序、中序、后序遍历的实现过程,并通过具体代码实例进行了演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树的递归遍历写起来很简单,但是往往在面试中要写出它的非递归遍历,然而很多人都不能够写出来。结合网上的资料,自己写了一个完整的二叉树的前序遍历、中序遍历和后序遍历作为总结。

二叉树的测试用例如下:



public class FindPath_25 {
	
	public static void main(String []args) {
		BinaryTreeNode node1 = new BinaryTreeNode(10);
		BinaryTreeNode node2 = new BinaryTreeNode(5);
		BinaryTreeNode node3 = new BinaryTreeNode(12);
		BinaryTreeNode node4 = new BinaryTreeNode(4);
		BinaryTreeNode node5 = new BinaryTreeNode(7);
		
		node1.connection(node1, node2, node3);
		node1.connection(node2, node4, node5);

		System.out.println("先序遍历:");
		preOrder(node1);
		System.out.println();
		System.out.println("中序遍历:");
		inorder(node1);
		System.out.println();
		System.out.println("后序遍历:");
		postOrder(node1);
		System.out.println();
	}

	//非递归后序遍历
	private static void postOrder(BinaryTreeNode node1) {
		BinaryTreeNode q = node1;
        Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
        while (node1 != null) {
            // 左子树入栈
            while(node1!=null) {
            	stack.push(node1);
            	node1 = node1.left;
            }
            node1 = stack.pop();
            // 当前节点无右子结点或右子结点已经输出
            while (node1 != null && (node1.right == null || node1.right == q)) {
                System.out.print(node1.data+" ");
                q = node1;// 记录上一个已输出节点
                if (stack.empty())
                    return;
                node1 = stack.pop();
            }
            // 处理右子树
            stack.push(node1);
            node1 = node1.right;
        }		
	}

	//非递归中序遍历
	private static void inorder(BinaryTreeNode node1) {
		Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
        while(node1!=null||!stack.empty()) {
        	if(node1!=null) {
        		stack.push(node1);
        		node1 = node1.left;
        	}else {
        		node1 = stack.pop();
        		System.out.print(node1.data+" ");
        		node1 = node1.right;
        	}
        }
	}

	//非递归先序遍历
	private static void preOrder(BinaryTreeNode node1) {
		Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
        if (node1 != null) {
            stack.push(node1);
            while (!stack.empty()) {
            	node1 = stack.pop();
            	System.out.print(node1.data+" ");
                if (node1.right != null)
                    stack.push(node1.right);
                if (node1.left != null)
                    stack.push(node1.left);
            }
        }
	}

}

class BinaryTreeNode{
    int data;
    BinaryTreeNode left;
    BinaryTreeNode right;
    
    BinaryTreeNode(int data) {
    	this.data = data;
    	left = null;
    	right = null;
    }
    
    public void connection(BinaryTreeNode root,BinaryTreeNode left,BinaryTreeNode right) {
    	if(root == null) return;
    	root.left = left;
    	root.right = right;
    }
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值