标签:
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if(root == null)
return ret;
ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>();
queue.add(root);
while(!queue.isEmpty())
{
ArrayList<Integer> currentLevel = new ArrayList<Integer>();
ArrayDeque<TreeNode> nextQueue = new ArrayDeque<TreeNode>();
TreeNode node;
while((node = queue.pollFirst())!=null)
{
currentLevel.add(node.val);
if(node.left!=null)
{
nextQueue.add(node.left);
}
if(node.right!=null)
{
nextQueue.add(node.right);
}
}
ret.add(currentLevel);
queue = nextQueue;
}
return ret;
}
Binary Tree Level Order Traversal
标签:
原文地址:http://www.cnblogs.com/neweracoding/p/4825084.html