# Middle-题目70：103. Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
/ \
9  20
/  \
15   7

return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {

boolean isReverse = true;
List<List<Integer>> result=new ArrayList();
List<Integer> temp=new ArrayList<Integer>();
if(root==null)
return result;
while(!queue.isEmpty())
{
TreeNode current=queue.remove();
int curLevel=levelqueue.remove();
//System.out.println(current.val+" level="+curLevel);

if(curLevel==result.size())
{
}
else
{
if(isReverse)
Collections.reverse(temp);
isReverse = !isReverse;
temp=new ArrayList<Integer>();
}
if(current.left!=null)
{
}
if(current.right!=null)
{
}
}
if(isReverse)
Collections.reverse(temp);
result.remove(0);
//  Collections.reverse(result);
return result;
}
}

4ms,beats 4.14%,众数3ms,45.49%

• 本文已收录于以下专栏：

举报原因： 您举报文章：Middle-题目70：103. Binary Tree Zigzag Level Order Traversal 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)