标签: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