二叉树按层打印

Posted figsprite

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树按层打印相关的知识,希望对你有一定的参考价值。

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


class TreeNode {
	int val = 0;
	TreeNode left = null;
	TreeNode right = null;

	public TreeNode(int val) {
		this.val = val;
	}

}

public class Solution {
	static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
		if(pRoot==null) {
			return null;
		}
		ArrayList<Integer> list = new ArrayList<>();
		ArrayList<ArrayList<Integer> > resList = new ArrayList<>();
		Deque<TreeNode> queue = new LinkedList<>();
		queue.offer(pRoot);
		int start=0;
		int end=1;
		while(!queue.isEmpty()) {
			TreeNode curNode = queue.poll();
			start++;
			list.add(curNode.val);
			if(curNode.left!=null) {
				queue.offer(curNode.left);
			}
			if(curNode.right!=null) {
				queue.offer(curNode.right);
			}
			
			if(start==end) {
				end=queue.size();
				start=0;
				resList.add(list);
				list = new ArrayList<>();
			}
		}
		return resList;
		
	}
	public static void main(String[] args) {
		TreeNode n1 = new TreeNode(1);
		TreeNode n2 = new TreeNode(2);
		TreeNode n3 = new TreeNode(3);
		TreeNode n4 = new TreeNode(4);
		TreeNode n5 = new TreeNode(5);
		TreeNode n6 = new TreeNode(6);
		TreeNode n7 = new TreeNode(7);
		TreeNode n8 = new TreeNode(8);

		n1.left=n2;
		n1.right = n3;
		n2.left=n4;
		n3.left=n5;
		n3.right=n6;
		n5.left=n7;
		n5.right=n8;
		
		ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<>();
		arrayLists= Print(n1);
		//System.out.println(arrayLists.size());
		for(ArrayList<Integer> a:arrayLists) {
			System.out.println(a);
		}
	}
}

  

import java.util.ArrayList; import java.util.Deque; import java.util.LinkedList; class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public class Solution { static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) { if(pRoot==null) { return null; } ArrayList<Integer> list = new ArrayList<>(); ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<>(); Deque<TreeNode> queue = new LinkedList<>(); TreeNode last = pRoot; TreeNode nLast = null; queue.offer(pRoot); while(!queue.isEmpty()) { pRoot=queue.poll(); list.add(pRoot.val); if(pRoot.left!=null) { queue.offer(pRoot.left); nLast = pRoot.left; } if(pRoot.right!=null) { queue.offer(pRoot.right); nLast=pRoot.right; } if(pRoot==last&&!queue.isEmpty()) { ArrayList<Integer> help = new ArrayList<>(); for(Integer a:list) { //System.out.print(a+" "); help.add(a); } arrayLists.add(help); list.clear(); //System.out.println(); last=nLast; } } return arrayLists; } public static void main(String[] args) { TreeNode n1 = new TreeNode(1); TreeNode n2 = new TreeNode(2); TreeNode n3 = new TreeNode(3); TreeNode n4 = new TreeNode(4); TreeNode n5 = new TreeNode(5); TreeNode n6 = new TreeNode(6); TreeNode n7 = new TreeNode(7); TreeNode n8 = new TreeNode(8); n1.left=n2; n1.right = n3; n2.left=n4; n3.left=n5; n3.right=n6; n5.left=n7; n5.right=n8; ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<>(); arrayLists= Print(n1); //System.out.println(arrayLists.size()); for(ArrayList<Integer> a:arrayLists) { System.out.println(a); } } }

  

以上是关于二叉树按层打印的主要内容,如果未能解决你的问题,请参考以下文章

c++二叉树按层序遍历顺序输入(由上到下),先序遍历顺序输出,求完整代码

二叉树按层遍历

从上往下打印二叉树-剑指Offer

二叉树相关算法设计题

二叉树按行打印,换行。。。。

北邮OJ 100. 二叉树的层数