标签:class lin evel eve 分享 view bsp int list
1 102 Binary Tree Level Order Traverse
public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); List<List<Integer>> res = new LinkedList<>(); if (root == null) return res; queue.offer(root); while (!queue.isEmpty()) { int num = queue.size(); List<Integer> list = new LinkedList<>(); for (int i = 0; i < num; i++) { if (queue.peek().left != null) queue.offer(queue.peek().left); if (queue.peek().right != null) queue.offer(queue.peek().right); list.add(queue.poll().val); } res.add(list); } return res; } }
标签:class lin evel eve 分享 view bsp int list
原文地址:http://www.cnblogs.com/whesuanfa/p/6850152.html