二叉树非递归遍历Java实现

二叉树的前序、中序和后序遍历可以采用递归和非递归的方法实现,递归的方法逻辑简单清晰,易于理解,但递归的方法需要使用额外的栈空间,运行效率较低。而非递归的方法则效率较高。


维基百科上有递归和非递归二叉树三种遍历实现的伪代码,https://en.wikipedia.org/wiki/Tree_traversal#In-order_2

下面是相应的Java实现:


前序遍历(非递归实现):

public static void preOrder(TreeNode x)
	{
		System.out.println();
		if(x!=null)
		{
			LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
			
			stack.addLast(x);
			while(!stack.isEmpty())
			{
				TreeNode temp=stack.removeLast();
				System.out.print(temp.val+" ");
				
				if(temp.rchild!=null)
				{
					stack.addLast(temp.rchild);
				}
				
				if(temp.lchild!=null)
				{
					stack.addLast(temp.lchild);	
				}
				
			}
			System.out.println();
		}
	}



中序遍历(非递归实现):

public static void inOrder(TreeNode x)
	{
		System.out.println();
		if(x!=null)
		{
			LinkedList<TreeNode> stack=new LinkedList<TreeNode>();

			while(!stack.isEmpty()||x!=null)
			{
				if(x!=null)
				{
					stack.addLast(x);
					x=x.lchild;
				}
				else
				{
					x=stack.removeLast();
					System.out.print(x.val+" ");
					x=x.rchild;
				}
			}
			System.out.println();
		}
	}
	



后序遍历(非递归实现):

public static void postOrderII(TreeNode x)
	{
		LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
		TreeNode lastNodeVistited=null;
		while(!stack.isEmpty()||x!=null)
		{
			if(x!=null)
			{
				stack.addLast(x);
				x=x.lchild;
			}
			else
			{
				TreeNode top=stack.getLast();
				if(top.rchild!=null&&lastNodeVistited!=top.rchild)
				{
					x=top.rchild;
				}
				else
				{
					System.out.print(top.val+" ");
					lastNodeVistited=stack.removeLast();
				}
			}
		}
		System.out.println();
	}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java二叉树非递归遍历可以通过使用栈数据结构来实现。首先,我们创建一个空的栈,将根节点入栈。然后,我们进入一个循环,直到栈为空为止。在每一次循环中,我们弹出栈顶元素,并将其访问。接下来,如果该节点有右子节点,则将右子节点入栈。如果该节点有左子节点,则将左子节点入栈。由于栈是先进后出的数据结构,所以我们先入栈右子节点,再入栈左子节点,以确保在遍历过程中先访问左子树节点。这样就能够实现二叉树非递归遍历。 以下是一个示例代码实现二叉树非递归中序遍历: ```java public void inorderTraversal(Node root) { if (root == null) { return; } Stack<Node> stack = new Stack<>(); Node current = root; while (current != null || !stack.isEmpty()) { while (current != null) { stack.push(current); current = current.leftChild; } current = stack.pop(); System.out.print(current.data + " "); // 访问节点 current = current.rightChild; } } ``` 在这个示例代码中,我们首先判断当前节点是否为空或者栈是否为空,如果不满足则进入循环。在循环内部,我们首先将当前节点及其所有左子节点入栈,直到当前节点为空。然后,我们弹出栈顶节点并访问该节点。最后,将当前节点更新为其右子节点,并继续下一次循环。 通过这种方式,我们可以实现二叉树非递归中序遍历。你可以根据需要修改代码实现其他类型的非递归遍历,比如前序遍历和后序遍历。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [用Python实现二叉树二叉树非递归遍历及绘制的例子](https://download.csdn.net/download/weixin_38618784/14869891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java实现二叉树树的非递归遍历](https://blog.csdn.net/weixin_41826973/article/details/105555647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值