-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution32.java
More file actions
30 lines (27 loc) · 788 Bytes
/
Copy pathsolution32.java
File metadata and controls
30 lines (27 loc) · 788 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
package nowcoder;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class solution32 {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> ret = new ArrayList<>();
queue.add(root);
while(!queue.isEmpty())
{
int cnt = queue.size();
while(cnt-->0)
{
TreeNode t = queue.poll();
if(t == null)
{
continue;
}
ret.add(t.val);
((LinkedList<TreeNode>) queue).add(t.left);
((LinkedList<TreeNode>) queue).add(t.right);
}
}
return ret;
}
}