码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树按层打印

时间:2019-04-11 21:02:38      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:amp   deque   nod   this   offer   last   end   root   stat   

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); } } }

  

二叉树按层打印

标签:amp   deque   nod   this   offer   last   end   root   stat   

原文地址:https://www.cnblogs.com/figsprite/p/10692447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!