import java.util.ArrayList;import java.util.Collections;import java.util.LinkedList;import java.util.Queue;/** * 面试题61:按之字形顺序打印二叉树 * 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。 */public class _61_font { public static void main(String[] args) { TreeNode61 treeNode61 = new TreeNode61(1); treeNode61.left= new TreeNode61(2); treeNode61.right= new TreeNode61(3); treeNode61.left.left= new TreeNode61(4); Solution61 solution61 = new Solution61(); ArrayList > print = solution61.Print(treeNode61); for(ArrayList a:print){ for(Integer b:a){ System.out.print(b+"、"); } System.out.println(); } }}class Solution61 { public ArrayList > Print(TreeNode61 pRoot) { ArrayList > arrayList = new ArrayList >(); if (pRoot == null) { return arrayList; } Queue queue=new LinkedList (); ArrayList row=new ArrayList (); queue.add(pRoot); boolean flag=false; while(!queue.isEmpty()){ row=new ArrayList (); //每次重新生成一个对象 int len=queue.size(); row.clear(); for(int i=0;i