题目:
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] ]
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
分析:
层次遍历稍微变了下形状而已
用一个变量记录每层的节点个数就ok了
参考代码:
http://blog.csdn.net/linhuanmars/article/details/24509105
这里LZ用了两个栈!
/**
* 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) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root==null)
return res;
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
int level=1;
ArrayList<Integer> item = new ArrayList<Integer>();
item.add(root.val);
res.add(item);
stack.push(root);
while(!stack.isEmpty())
{
LinkedList<TreeNode> newStack = new LinkedList<TreeNode>();
item = new ArrayList<Integer>();
while(!stack.isEmpty())
{
TreeNode node = stack.pop();
if(level%2==0)
{
if(node.left!=null)
{
newStack.push(node.left);
item.add(node.left.val);
}
if(node.right!=null)
{
newStack.push(node.right);
item.add(node.right.val);
}
}
else
{
if(node.right!=null)
{
newStack.push(node.right);
item.add(node.right.val);
}
if(node.left!=null)
{
newStack.push(node.left);
item.add(node.left.val);
}
}
}
level++;
if(item.size()>0)
res.add(item);
stack = newStack;
}
return res;
}
}