标签:new 函数 ack nbsp bsp util import solution roo
import java.util.ArrayList; import java.util.Stack; /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) { Stack<TreeNode> stack1 = new Stack<>(); stack1.push(pRoot); int layer = 1; Stack<TreeNode> stack2 = new Stack<>(); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); while(!stack1.empty() || !stack2.empty()) { if(layer%2 != 0) { ArrayList<Integer> temp = new ArrayList<>(); while(!stack1.empty()) { TreeNode node = stack1.pop(); if(node != null) { temp.add(node.val); stack2.push(node.left); stack2.push(node.right); } } if(!temp.isEmpty()) { list.add(temp); layer++; } }else { ArrayList<Integer> temp = new ArrayList<>(); while(!stack2.empty()) { TreeNode node = stack2.pop(); if(node != null) { temp.add(node.val); stack1.push(node.right); stack1.push(node.left); } } if(!temp.isEmpty()) { list.add(temp); layer++; } } } return list; } }
标签:new 函数 ack nbsp bsp util import solution roo
原文地址:https://www.cnblogs.com/yihangZhou/p/10515629.html