-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetcode_102_33.java
More file actions
35 lines (34 loc) · 1002 Bytes
/
Leetcode_102_33.java
File metadata and controls
35 lines (34 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* @lc app=leetcode id=102 lang=java
*
* [102] Binary Tree Level Order Traversal
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if(root == null) return list;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> levelList=new ArrayList<Integer>();
for(int i = 0; i < size; i++){
TreeNode node = queue.poll();
levelList.add(node.val);
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
}
list.add(levelList);
}
return list;
}
}