标签:style blog java color io for
java很给力逆序用了Colletcions.reverse();
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { ArrayList<ArrayList<Integer>> ret=new ArrayList<ArrayList<Integer>>(); if(root==null) return (List)ret; Queue<TreeNode> q=new LinkedList<TreeNode>(); boolean flag=true; q.offer(root); while(!q.isEmpty()) { int len=q.size(); ArrayList<Integer> temp=new ArrayList<Integer>(); for(int i=0;i<len;i++) { TreeNode t=q.poll(); temp.add(t.val); if(t.left!=null)q.offer(t.left); if(t.right!=null) q.offer(t.right); } if(!flag) Collections.reverse(temp); ret.add(temp); flag=!flag; } return (List)ret; } }
Binary Tree Zigzag Level Order Traversal,布布扣,bubuko.com
Binary Tree Zigzag Level Order Traversal
标签:style blog java color io for
原文地址:http://www.cnblogs.com/hansongjiang/p/3859399.html