103. 二叉树的锯齿形层序遍历
用了俩个队列和一个栈
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> list=new ArrayList<>();
Queue<TreeNode> queue1=new LinkedList<>();
Queue<TreeNode> queue2=new LinkedList<>();
Stack<Integer> stack=new Stack<>();
if(root==null)
return list;
queue1.offer(root);
while(!queue1.isEmpty()||!queue2.isEmpty()){
List<Integer> lists=new ArrayList<>();
while(!queue1.isEmpty()){
TreeNode p1=queue1.poll();
lists.add(p1.val);
if(p1.left!=null)
queue2.offer(p1.left);
if(p1.right!=null)
queue2.offer(p1.right);
}
if(lists.size()!=0)
list.add(lists);
List<Integer> listss=new ArrayList<>();
while(!queue2.isEmpty()){
TreeNode p2=queue2.poll();
stack.push(p2.val);
if(p2.left!=null)
queue1.offer(p2.left);
if(p2.right!=null)
queue1.offer(p2.right);
}
while(!stack.isEmpty()){
listss.add( stack.pop());
}
if(listss.size()!=0)
list.add(listss);
}
return list;
}
}