码迷,mamicode.com
首页 > 其他好文 > 详细

Binary Tree Zigzag Level Order Traversal

时间:2014-07-22 00:04:35      阅读:154      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!