二叉树的层次遍历(打印)

一:按层打印

按层打印原本是十分基础的内容,本例将同一层的节点打印在同一行上,并输出行号。关键问题是换行的时机。只需要用两个node类型的变量last和nLast即可解决这个问题。

last变量表示正在打印的当前行的最右节点,nLast表示下一行的最右节点。假设我们每一层都做从左到右的宽度优先遍历,如果发现遍历到的节点等于last,说明该换行了。换行之后只要令last = nLast,就可以继续下一行的打印过程。

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

public class LevelTravel {
	
	public static void main(String[] args) {
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);
		Node node4 = new Node(4);
		Node node5 = new Node(5);
		Node node6 = new Node(6);
		Node node7 = new Node(7);
		Node node8 = new Node(8);
		node1.left = node2;
		node1.right = node3;
		node2.left = node4;
		node3.left = node5;
		node3.right = node6;
		node5.left = node7;
		node5.right = node8;
		levelTravel(node1);
	}
	
	public static void levelTravel(Node root) {
		if(root == null) {
			return;
		}
		Queue<Node> queue = new LinkedList<Node>();
		
		int level = 1;
		Node last = root;//当前行的最右节点
		Node nLast = null;//下一行的最右节点
		
		queue.offer(root);
		System.out.print("Level " + (level++) + " : ");
		while(!queue.isEmpty()) {
			root = queue.poll();
			System.out.print(root.value + " ");
			if(root.left != null) {
				queue.offer(root.left);
				nLast = root.left;
			}
			if(root.right != null) {
				queue.offer(root.right);
				nLast = root.right;
			}
			//遍历到当前行的最右节点,换行
			if(root == last && !queue.isEmpty()) {
				System.out.print("\nLevel " + (level++) + " : ");
				last = nLast;//记录下一行的最右节点,继续下一行的打印
			}
		}
	}

}

二:之字形打印

原则一:如果是从左到右,一律从dq的头部弹出节点,如果弹出的节点没有孩子节点,不用放任何节点到dq中,如果当前节点有孩子节点,先让左孩子从尾部进入dq,再让右孩子从尾部进入dq。

原则二:如果是从右到左,一律从dq的尾部弹出节点,如果弹出的节点没有孩子节点,不用放任何节点到dq中,如果当前节点有孩子节点,先让右孩子从头部进入dq,再让左孩子从头部进入dq。

用原则一和原则二的切换,即可完成之字形打印,现在只剩一个问题,如何确定切换原则1和原则2的时机,其实还是确定每一层最后一个节点的问题。在打印过程中,下一层最后一个节点是当前层有孩子的节点中最先进入dq的孩子节点。


import java.util.Deque;
import java.util.LinkedList;

public class ZigZagTravel {
	
	public static void main(String[] args) {
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);
		Node node4 = new Node(4);
		Node node5 = new Node(5);
		Node node6 = new Node(6);
		Node node7 = new Node(7);
		Node node8 = new Node(8);
		node1.left = node2;
		node1.right = node3;
		node2.left = node4;
		node3.left = node5;
		node3.right = node6;
		node5.left = node7;
		node5.right = node8;
		zigZagTravel(node1);
	}
	
	public static void zigZagTravel(Node root) {
		if(root == null) {
			return;
		}
		Deque<Node> deque = new LinkedList<Node>();
		int level = 1;
		boolean lr = true;
		Node last = root;
		Node nLast = null;
		deque.offerFirst(root);
		printLevelAndDirection(lr, level++);
		while(!deque.isEmpty()) {
			if(lr) {
				root = deque.pollFirst();
				if(root.left != null) {
					nLast = nLast == null ? root.left : nLast;//下一层最后打印的节点是当前层有孩子节点的最先进入deque的孩子节点
					deque.offerLast(root.left);
				} 
				if(root.right != null) {
					nLast = nLast == null ? root.right : nLast;
					deque.offerLast(root.right);
				}
			} else {
				root = deque.pollLast();
				if(root.right != null) {
					nLast = nLast == null ? root.right : nLast;
					deque.offerFirst(root.right);
				} 
				if(root.left != null) {
					nLast = nLast == null ? root.left : nLast;
					deque.offerFirst(root.left);
				}
			}
			System.out.print(root.value + " ");
			if(root == last && !deque.isEmpty()) {
				lr = !lr;//到当前层最后一个节点,换方向
				last = nLast;//记录下一行最后一个节点,继续下一行的打印
				nLast = null;//清空nLast,
				System.out.println();
				printLevelAndDirection(lr, level++);
			}
		}
	}
	
	public static void printLevelAndDirection(boolean lr, int level) {
		System.out.print("Level: " + level + " from ");
		System.out.print(lr ? "left to right: " : "right to left: ");
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值