之子形打印二叉树
import java.util.*; /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ /* 从返回值我们得知每层都要存一个ArrayList<Integer> 然后再向ArrayList<ArrayList<Integer>>中存入每一层的ArrayList<Integer> */ public class Solution { public static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) { int layer = 1; //s1存奇数层节点 Stack<TreeNode> s1 = new Stack<TreeNode>(); s1.push(pRoot); //s2存偶数层节点 Stack<TreeNode> s2 = new Stack<TreeNode>(); ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); while (!s1.empty() || !s2.empty()) { if (layer%2 != 0) { ArrayList<Integer> temp = new ArrayList<Integer>(); while (!s1.empty()) { TreeNode node = s1.pop(); if(node != null) { temp.add(node.val); System.out.print(node.val + " "); s2.push(node.left); s2.push(node.right); } } if (!temp.isEmpty()) { list.add(temp); layer++; System.out.println(); } } else { ArrayList<Integer> temp = new ArrayList<Integer>(); while (!s2.empty()) { TreeNode node = s2.pop(); if(node != null) { temp.add(node.val); System.out.print(node.val + " "); s1.push(node.right); s1.push(node.left); } } if (!temp.isEmpty()) { list.add(temp); layer++; System.out.println(); } } } return list; } }