非递归方式实现二叉树的三种遍历

非递归方式实现二叉树的先序、中序、后序遍历:
  1. 任何递归函数都可以改成非递归
  2. 自己设计压栈来实现

先序遍历:

  1. 弹出就打印
  2. 如果有右孩子,压入右孩子
  3. 如果有左孩子,压入左孩子

后序遍历:需要用到两个栈

  1. 额外申请一个栈s2,弹出后放入s2中
  2. 如果有左孩子,压入左孩子
  3. 如果有右孩子,压入右孩子

中序遍历:

1)以cur为头的整棵树的整条左边界入栈,遇到空就从栈中弹出
2)弹出就打印,并且cur来到弹出结点的右孩子(如果没有右孩子就接着从栈中弹出数据),继续执行条件1),一直到栈为空

后续遍历另一种方法:只需要用到一个栈,另外设置两个变量h和c,h记录之前打印的结点的位置,c记录栈顶的位置。

package com.harrison.class07;

import java.util.Stack;

public class Code02_UnRecursiveTraversalBT {
	public static class Node{
		public int value;
		public Node left;
		public Node right;
		
		public Node(int v) {
			value=v;
		}
	}
	
	public static void pre(Node head) {
		System.out.println("pre-order:");
		if(head!=null) {
			Stack<Node> s=new Stack<>();
			s.push(head);
			while(!s.isEmpty()) {
				head=s.pop();
				System.out.print(head.value+" ");
				if(head.right!=null) {
					s.push(head.right);
				}
				if(head.left!=null) {
					s.push(head.left);
				}
			}
		}
		System.out.println();
	}
	
	public static void pos1(Node head) {
		System.out.println("pos-order:");
		if(head!=null) {
			Stack<Node> s1=new Stack<>();
			Stack<Node> s2=new Stack<>();
			s1.push(head);
			while(!s1.isEmpty()) {
				head=s1.pop();
				s2.push(head);
				if(head.left!=null) {
					s1.push(head.left);
				}
				if(head.right!=null) {
					s1.push(head.right);
				}
			}
			while(!s2.isEmpty()) {
				System.out.print(s2.pop().value+" ");
			}
		}
		System.out.println();
	}
	
	public static void in(Node head) {
		System.out.println("in-order:");
		if(head!=null) {
			Stack<Node> s=new Stack<>();
			while(!s.isEmpty() || head!=null) {
				if(head!=null) {
					s.push(head);
					head=head.left;
				}else {
					head=s.pop();
					System.out.print(head.value+" ");
					head=head.right;
				}
			}
		}
		System.out.println();
	}
	
	public static void pos2(Node h) {
		System.out.println("pos-order:");
		if(h!=null) {
			Stack<Node> stack=new Stack<>();
			stack.push(h);
			Node c=null;
			while(!stack.isEmpty()) {
				c=stack.peek();
				if(c.left!=null && h!=c.left && h!=c.right) {
					stack.push(c.left);
				}else if(c.right!=null && h!=c.right) {
					stack.push(c.right);
				}else {
					System.out.print(stack.pop().value+" ");
					h=c;
				}
			}
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		Node head = new Node(1);
		head.left = new Node(2);
		head.right = new Node(3);
		head.left.left = new Node(4);
		head.left.right = new Node(5);
		head.right.left = new Node(6);
		head.right.right = new Node(7);

		pre(head);
		System.out.println("========");
		in(head);
		System.out.println("========");
		pos1(head);
		System.out.println("========");
		pos2(head);
		System.out.println("========");
	}

}

  • 18
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱敲代码的Harrison

从来无所求,所得皆惊喜。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值