二叉树遍历(递归和非递归)


二叉树的节点定义如下:

//节点
class TreeNode{
	int val;
	TreeNode left,right;
	TreeNode(int val){
		this.val=val;
	}
}

在这里插入图片描述

二叉树的前序遍历

二叉树的前序遍历顺序为:根左右。如下图所示,前序遍历顺序为:1245367。

递归

递归代码:

public static void preOrder(TreeNode root){
		if(root==null){
			return;
		}
		System.out.print(root.val+" ");
		preOrder(root.left);
		preOrder(root.right);
	}

非递归

非递归实现主要用到了Stack容器。

public static void preOrder2(TreeNode root){
		if(root==null){
			return;
		}
		Stack<TreeNode> stack=new Stack<>();
		while(!stack.isEmpty() || root!=null){
			if(root!=null){
				//打印根的值
				System.out.print(root.val+" ");
				//压入当前节点
				stack.push(root);
				//往左子树方向走
				root=root.left;
			}else{
				//弹出节点
				TreeNode node=stack.pop();
				root=node.right;
			}	
		}
	}

二叉树的中序遍历

二叉树的中序遍历顺序为:左根右。如下图所示,中序遍历顺序为:4251637 。

递归

public static void midOrder(TreeNode root){
		if(root==null){
			return;
		}
		midOrder(root.left);
		System.out.print(root.val+" ");
		midOrder(root.right);
	}

非递归

非递归实现主要用到了Stack容器。

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

二叉树的后序遍历

二叉树的后序遍历顺序为:左右根。如下图所示,中序遍历顺序为:4526731 。

递归

public static void postOrder(TreeNode root){
		if(root==null){
			return;
		}
		postOrder(root.left);
		postOrder(root.right);
		System.out.print(root.val+" ");
	}

非递归

非递归实现主要用到了Stack容器.

public static void postOrder2(TreeNode root){
		if(root==null){
			return;
		}
		Stack<TreeNode> stack=new Stack<>();
		//记录前一个输出过的节点
		TreeNode preNode=null;
		while(!stack.isEmpty() || root!=null){
			if(root!=null){
				//不断往左子节点走
				stack.push(root);
				root=root.left;
			}else{
				TreeNode node=stack.peek();
				//如果当前节点有右子节点,且右子节点没有被访问过
				if(node.right!=null && node.right!=preNode){
					root=node.right;
				}else{
					//当前节点没有右子节点或者右子节点被访问过,则弹出。
					stack.pop();
					System.out.print(node.val+" ");
					//标记访问过的节点
					preNode=node;
					root=null;
				}
			}
		}
	}

测试

代码:

class TreeNode{
	int val;
	TreeNode left,right;
	TreeNode(int val){
		this.val=val;
	}
}
//建树
class Tree{
	
}
public class Test{
	public static void main(String[] args){
		TreeNode root=createTree();
		System.out.println("前序遍历(递归):");
		preOrder(root);
		System.out.println();
		System.out.println("前序遍历(非递归):");
		preOrder2(root);
		System.out.println();
		
		System.out.println("中序遍历:");
		midOrder(root);
		System.out.println();
		System.out.println("中序遍历(非递归):");
		midOrder2(root);
		System.out.println();
		
		System.out.println("后序遍历:");
		postOrder(root);
		System.out.println();
		System.out.println("后序遍历(非递归):");
		postOrder2(root);
		
	}
	public static TreeNode createTree(){
		TreeNode root=new TreeNode(1);
		TreeNode node1=new TreeNode(2);
		TreeNode node2=new TreeNode(3);
		root.left=node1;
		root.right=node2;
		node1.left=new TreeNode(4);
		node1.right=new TreeNode(5);
		node2.left=new TreeNode(6);
		node2.right=new TreeNode(7);
		return root;
	}
	//前序遍历
	public static void preOrder(TreeNode root){
		//....
	}
	
	//前序遍历 非递归
	public static void preOrder2(TreeNode root){
		//...
	}
	
	//中序遍历
	public static void midOrder(TreeNode root){
		//...
	}
	
	//中序遍历 非递归
	public static void midOrder2(TreeNode root){
		//...
	}
	
	//后续遍历
	public static void postOrder(TreeNode root){
		//...
	}
	
	//后续遍历 非递归
	public static void postOrder2(TreeNode root){
		//...
	}
}

结果如下:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值